Thread: Different question about inheritance

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Different question about inheritance

    Here is what I'm trying to do:
    Create a Vector of a parent class, and fill it up with different children of that class, then make a loop which cicles through the vector and calls a method from each class. The problem is that even if the child class has overriden the method, it keeps calling the parent's method. The method is declared as virtual in both the parent and the child class isn't that how it should be?

    I'm absolutely sure the method has the same signature: return type, name and parameters (in this case none), this is correct, right?

    Here is the section I'm having trouble with
    Code:
    void CSDL::render(std::vector<Entity> entities)
    {    
        Uint32 color;
        
        color = SDL_MapRGB(m_screen->format, 0, 0, 0);
        SDL_FillRect(m_screen, NULL, color); 
        
        for (int i = 0; i < entities.size(); i++)
        {
            drawSprite(entities[i].getX(), entities[i].getY(),
                       entities[i].getSprite());
            entities[i].update(); // <--- This is the method it should call from the child class, but is calling from the parent.
        }
    
        SDL_Flip(m_screen);
    }
    Why drink and drive when you can smoke and fly?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The method is declared as virtual in both the parent and the child class isn't that how it should be?
    Yes, but that is not sufficient. You should learn to use virtual functions in a simpler setting. Open your book to the chapter on virtual functions and polymorphism and write a small program with a simple base class that has one virtual function and a derived class that overrides the virtual function and get that to work.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem is that you are holding Base class objects in your vector instead of pointers or references. That means that when you add a derived class object to the vector, its derived-ness gets sliced off and only the Base class part of the object is stored in the vector.

    Since you cannot hold references in a vector, the solution is to hold pointers. This means that you have to allocate each derived object with new and save the pointer in the vector. Then whenever you erase an element from the vector or whenever you clear() the vector (or when the vector is destructed) you have to remember to call delete on the pointers.

    Note that the destructor of your base class must be virtual, which should be done anyways.

    There are other more elegant solutions than storing the raw pointer in the vector, which include storing a boost::shared_ptr of the base class, or using a boost ptr_container.

    Also note that this is not a problem specific to the vector, it would happen with a C array or any other container that stores your objects.

  4. #4
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    exactly as what daved said, have a pointer to the base class that equals a pointer to the derived class on the stack

    BaseClass* Test = new DerivedClass;

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved
    There are other more elegant solutions than storing the raw pointer in the vector, which include storing a boost::shared_ptr of the base class, or using a boost ptr_container.
    hey man, that's not cool. You're stealing my position as forum boost \/\/h0re...

    [edit]awww it would'nt let me say \/\/h0re!!
    Last edited by ChaosEngine; 03-02-2006 at 03:50 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks a bunch everyone, especially Daved. I used your first suggestion and it works perfectly.
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question about templates & inheritance
    By blacknail in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2008, 01:51 PM
  2. Virtual inheritance
    By 6tr6tr in forum C++ Programming
    Replies: 13
    Last Post: 05-07-2008, 11:20 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM