concerning virtual functions

This is a discussion on concerning virtual functions within the C++ Programming forums, part of the General Programming Boards category; hi, this is a little complicated to explain, so i'm just going to do the best i can. To summarize ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    concerning virtual functions

    hi, this is a little complicated to explain, so i'm just going to do the best i can.

    To summarize my problem, i have a base class with a virtual function (which is overridden in a derived class).

    I need a function in the base class to call the correct version of the virtual function. (in this case, the one in the derived class)

    here's a basic example:

    Code:
    class Base
    {
    public:
      ~Base();
       static Derived *child;
       void callRelease();
       virtual void Release(int value);
    }
    
    class Derived : public Base
    {
    public:
       virtual void Release(int value);
    }
    
    
    /////////////// Base Member Functions ////////////////////
    Base::~Base()
    {
      callRelease();   
    }
    
    void Base::callRelease()
    {
       child->Release(3);  //PROBLEM: I need this to call the Derived::Release(3)!!
    }
    void Base::Release(int value)
    {
       //basic release stuff
    }
    
    ////////////Derived Member Function/////////////////
    void Derived::Release(int value)
    {
       //more advanced, Derived-specific, release stuff
    }
    
    
    /////////////Main Function///////////////
    
    Derived *Base::child; //initialize the static variable, 
                                      //(points to derived)
    
    void main()
    {
         Derived *d = new Derived();
         Base::child = d;  //the child static variable now points to d
      
         delete d;  
    }
    Heres what happens:
    - d is created and instanciated with a new Derived class;
    - d is deleted, first calling the empty Derived destructor
    - d then calls the Base destructor, which calls CallRelease();

    Heres where the problem is:

    at this point, I need CallRelease to call the specific version of release, Derived::Release(int value).

    I assumed that because the static pointer was to a derived class,
    child->Release(3) would call Derived::Release();

    Instead, it calls Base::Release(3), and i can't figure out why, or how to fix it.


    sorry that may have been hard to follow, and thanks for the help in advance.

    it's probably something stupid (as it always is)

    but how do i fix it?!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    First, your destructor must be virtual in the base class. Always make the base class destructor virtual if you will use the class polymorphically as you are doing here. If you don't have a virtual destructor in the base class and you delete a base class pointer that points to an instance of the derived class, it is undefined behavior and could cause problems with your program (but often will just fail to destroy the derived class properly).

    Second, assuming the destructors are done correctly, the derived part of the class is destroyed before the base part of the class, so you should not be calling virtual methods in the base class destructor. A better solution might be to do the "releasing" in the derived destructor instead of a separate method.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    ah. should all base class constructors be virtual too?
    i wasnt sure if it would work that way, because they technically had different names.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    A constructor cannot be virtual (it won't compile). It doesn't make sense because the point of virtual is so that the correct derived class version is called if you are using a base class pointer, and a constructor is called when you create the object, at which point you know the correct derived type already.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Great, it all seems to be working.
    Thanks a ton!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 10:48 AM
  2. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 03:13 PM
  3. Thread creation on virtual functions
    By gustavosserra in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2004, 08:03 AM
  4. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21