CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1

    Constructors and Destructors - calling virtual functions....

    I have a question about constructors and destructors calling virtual functions. The code to a simple program I am attaching illustrates my question.

    I'd like to know what the standard says about this, because from the information I know - it appears as a VC++ bug. (version 6.0) I say that, because there is a switch in the compiler options for turning on and off the vtable member, and it says only disable this if you are sure constructors and destructors call virtual functions virtually. It does it with the member on or off - failing to call the derived overloaded functions and calling the base class version both times.
    Last edited by JamesSchumacher; September 2nd, 2002 at 01:17 PM.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    No. You can't call the 'derived' virtual function from a base class ctor or dtor. The simple reason is that, whilst you are in a ctor or dtor the object does not exist. An object's lifetime is from the time the ctor exits to the time it enters the dtor. More specifically, in the base class ctor, the derived ctor has not yet been called, and in the base class dtor, the derived class dtor has already been called. The best you can expect is for it to call the base class version of the virtual function. Which can be problematic if you have this:
    Code:
    class base
    {
    public:
        base() {  v1(); }
        virtual void v1() = 0;
    };
    
    class derived : public base
    {
    public:
        virtual void v1() { /* something */ }
    };
    Expect fireworks.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3

    Okay....

    I was just confused because MS has that option I stated above that says "constructors and destructors to call virtual functions virtually." - Which is saying to me - that constructors and destructors can call virtual functions. Then again, I know MS is hardly the people to look at for any standard issue.

    Although, with your code example I thought you had to 'virtual' inherit from a base with a pure virtual function? I've never had to use a base with pure virtual functions, so I have to go on what I know from reading. Okay, I tried it - no need for 'virtual' inheritance - I was confusing it with inheriting from two classes that inherit from the same base class. 'virtual' inheritance is for getting one copy of an ambiguity of deriving from two or more classes with a common base. Like having a car class as a base for all cars, a ford and chevy class both derived from car, and creating a hybrid derived from both ford and chevy - a need for virtual inheritance right there.
    Last edited by JamesSchumacher; September 2nd, 2002 at 01:44 PM.

  4. #4
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    I have written one complete artical on this which might be helpful to you. Take a look at

    http://www.codeguru.com/atl/ATL_UndertheHood_2.html

    Hope it helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured