Thread: Another quick question about virtual destructors and friend classes

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Another quick question about virtual destructors and friend classes

    If I have a base class with a virtual destructor and derive another class from it with it's own destructor, will the code I put in the base class' destructor also be called when I destroy the derived object?

    Also, if both the base class' default constructor and destructor are called when making/deleting an instance of a derived class, and these constructor and destructor call a protected member function of another class, do the derived classes have to be included as friends in that third class as well, or just the base class?

    i.e. will the following work?

    Code:
    class BaseClass
    {
      public:
        BaseClass();
        virtual ~BaseClass();
         ~other members~
      private
        int number;
    };
    
    class DerivedClass: public BaseClass
    {
      public:
        DerivedClass();
        virtual ~DerivedClass();
         ~other members~
    };
    
    class ListClass
    {
      public:
        friend class BaseClass;
      protected:
        int RegisterObject(BaseClass *objecttoregister);
        void UnregisterObject(int number);
    };
    
    ListClass ListClassObject;
    
    BaseClass::BaseClass() : number(ListClassObject.RegisterObject(this))
    { // can I even use 'this' here?
    }
    
    BaseClass::~BaseClass()
    {
      ListClassObject.UnregisterObject(number);
    }
    
    DerivedClass::DerivedClass()
    {
      //initialise DerivedClass' new variables
    }
    
    DerivedClass::~DerivedClass()
    {
      // clean up DerivedClass' variables
    }
    
    int main()
    {
      BaseClass *BaseClassPtr=new DerivedClass;
      // Do stuff
      delete BaseClassPtr;
      return 0;
    }
    Would this call ListClassObject.RegisterObject(this); and ListClassObject.UnregisterObject(number);, where 'this' is what will be BaseClassPtr, and 'number' is the return value of the former, like I'd expect it to?
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> will the code I put in the base class' destructor also be called when I destroy the derived object?
    Yes.

    >> do the derived classes have to be included as friends in that third class as well, or just the base class?
    No, just the base class. Only the class that that actually makes the call needs to be a friend.

    >> will the following work?
    Yes.

    >> // can I even use 'this' here?
    Maybe. I think it should be fine as long as you are not dereferencing the pointer inside RegisterObject.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Ah, thanks a bunch. For some reason I couldn't find this in any of the tutorials and FAQs I've been reading.
    Looks like this'll make things quite a bit more manageable.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

Popular pages Recent additions subscribe to a feed