Thread: Inheritance - Problem calling methods

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    Inheritance - Problem calling derived methods from base class pointer

    I have a class RAW_FILE which is the base class, and a class DIRECTORY which is derived from RAW_FILE.
    The code below shows my problem, and I would like to know if there is a way to solve it without just creating a virtual method (printToScreen) in the base class.

    In this code, I cannot assign the address held in temp_dir->next (RAW_FILE* next) to temp_dir (DIRECTORY* temp_dir).
    Note also that first_subdir is declared as 'DIRECTORY* first_subdir;'
    Code:
    unsigned int DIRECTORY::printToScreen(bool print_directories = false) {
    ...
            DIRECTORY *temp_dir = first_subdir;
            while(temp_dir != NULL) {
                counter = counter + temp_dir->printToScreen(print_directories);
                temp_dir = temp_dir->next; 
                // 'next' is a RAW_FILE pointer, so this causes a compile error when trying to
                // assign it to temp_dir (DIRECTORY POINTER)
            }
    }
    But if, I make temp_dir a RAW_FILE pointer, I cannot call the method printToScreen, because the method doesn't exist in RAW_FILE objects.
    Code:
    unsigned int DIRECTORY::printToScreen(bool print_directories = false) {
    ...
            RAW_FILE *temp_dir = first_subdir;
            while(temp_dir != NULL) {
                counter = counter + temp_dir->printToScreen(print_directories);
                //printToScreen is a method in the DIRECTORY class, so can't be called from
                //the base class RAW_FILE
                temp_dir = temp_dir->next;
            }
    }
    Code:
    class RAW_FILE {
    protected:
        char* path;
    public:
        RAW_FILE *previous, *next;
    };
    
    class DIRECTORY: public RAW_FILE {
    private:
        DIRECTORY *first_subdir, *last_subdir;
        RAW_FILE *first_file, *last_file;
    public:
        unsigned int printToScreen(bool);
    };
    Last edited by rusty_turkey; 05-30-2007 at 08:13 PM. Reason: Removing irrelavent code

  2. #2
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    If I'm understanding your problem correctly, you want this:
    Code:
    temp_dir = dynamic_cast<DIRECTORY*>(temp_dir->next);
    That will downcast RAW_FILE* to DIRECTORY* as long as temp_dir->next actually points to a DIRECTORY. If it doesn't, this will give you a NULL pointer.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    3
    Hmm... thanks for the suggestion, but I tried this:
    Code:
    DIRECTORY *temp_dir = first_subdir;
    while(temp_dir != NULL) {
        counter = counter + temp_dir->printToScreen(print_directories);
        temp_dir = dynamic_cast<DIRECTORY*>(temp_dir->next);
    }
    and got a compile error:
    cannot dynamic_cast `((RAW_FILE*)temp_dir)->RAW_FILE::next' (of type `class RAW_FILE*') to type `class DIRECTORY*' (source type is not polymorphic)

    Just to reiterate - RAW_FILE is the base class, DIRECTORY is the derived class.
    printToScreen is a method in DIRECTORY, but doesn't exist in RAW_FILE.

    temp_dir->next will always point to DIRECTORY objects at the point this code is run: BUT
    temp_dir->next has to stay as type RAW_FILE* because in other parts of the code it points to RAW_FILE objects

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    You aren't using private or protected inheritance, are you?

    Could you post the relevant parts of the class definitions?

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    3
    Quote Originally Posted by AverageSoftware View Post
    You aren't using private or protected inheritance, are you?

    Could you post the relevant parts of the class definitions?
    I've put the class definitions in the original post.

  6. #6
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    I did some experiments, and it looks like dynamic_cast will not work if the parent class doesn't contain any virtual functions.

    If you write a class that you know will be inherited from, it is good practice to provide a virtual destructor, even if it does nothing. This ensures proper deletion of objects if you delete them from a base class pointer.

    So if you add a virtual destructor to RAW_FILE, the dynamic_cast should work, or at least it does on GCC 4.0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple inheritance problem please help:P
    By miguel811 in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2009, 02:55 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem calling external method from within a shared library
    By Major Tom in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2007, 09:14 AM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Inheritance problem, help please!
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2002, 07:02 AM