Virtual base class

This is a discussion on Virtual base class within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Here is the related C++ Spec and my test code. I think virtual base class, no matter direct ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Virtual base class

    Hello everyone,


    Here is the related C++ Spec and my test code. I think virtual base class, no matter direct virtual base class or not, will always be constructed before non-virtual class (including non-virtual direct base class), correct?

    Another question is, what means "and only for the constructor of the most derived class as described below" in related Spec statements?

    Spec:

    --------------------
    12.6.2 Initializing bases and members [class.base.init]

    5 Initialization shall proceed in the following order:
    — First, and only for the constructor of the most derived class as described below, virtual base classes shall
    be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph
    of base classes, where “left-to-right” is the order of appearance of the base class names in the derived
    class base-specifier-list.
    — Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list
    (regardless of the order of the mem-initializers).
    — Then, nonstatic data members shall be initialized in the order they were declared in the class definition
    (again regardless of the order of the mem-initializers).
    — Finally, the body of the constructor is executed.
    [Note: the declaration order is mandated to ensure that base and member subobjects are destroyed in the
    reverse order of initialization. ]
    --------------------

    Code:
    /*
    output
    constructor B1
    constructor B2
    constructor D2
    constructor D1
    constructor D3
    */
    
    #include <iostream>
    
    using namespace std;
    
    class B1 {
    public:
    	 B1()
    	 {
    		 cout << "constructor B1 " << endl;
    	 }
    };
    
    class B2 {
    public:
    	 B2()
    	 {
    		 cout << "constructor B2 " << endl;
    	 }
    };
    
    class D1 : virtual public B1 {
    public:
    	 D1()
    	 {
    		 cout << "constructor D1 " << endl;
    	 }
    };
    
    class D2 : public virtual B1, public B2 {
    public:
    	 D2()
    	 {
    		 cout << "constructor D2 " << endl;
    	 }
    };
    
    class D3 : public D1, virtual public D2 {
    public:
    	 D3()
    	 {
    		 cout << "constructor D3 " << endl;
    	 }
    };
    
    int main()
    {
    	D3 d;
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This thread is a classic example of your bad learning methodology. Here you are, worrying about initialization order in an absurd hierarchy that you'll never encounter in the real world, instead of doing productive things, like writing code.

    I'm way too lazy to slog through the standardese here, but I can tell you this: the order of initialization will be the "right thing" to do. No class will be initialized before any of its base classes, and that's all you really need to know. If you have any other order dependencies, your code is already broken, whether it's legal or not.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I just want to make sure I understand the basic concepts in depth before I met with real issues. I am also interested in basic concepts -- which seems but not easy to make a real understand.

    what means "and only for the constructor of the most derived class as described below" in related Spec statements?

    Quote Originally Posted by CornedBee View Post
    This thread is a classic example of your bad learning methodology. Here you are, worrying about initialization order in an absurd hierarchy that you'll never encounter in the real world, instead of doing productive things, like writing code.

    I'm way too lazy to slog through the standardese here, but I can tell you this: the order of initialization will be the "right thing" to do. No class will be initialized before any of its base classes, and that's all you really need to know. If you have any other order dependencies, your code is already broken, whether it's legal or not.

    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It means that virtual bases aren't initialized twice.

    What you're trying to understand here isn't a basic concept. The basic concept is "initialization does the right thing" and "base classes before derived classes".
    What you're trying to understand is standardese.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by CornedBee View Post
    If you have any other order dependencies, your code is already broken, whether it's legal or not.
    Not true; a common purpose of software design is to achieve a set of observable effects in some defined order.

    In any event, George already quoted the relevant section of the standard.

    Constructor of the most derived class means the class that is being instantiated.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee and grumpy,


    I have some re-thinking about this statement. I think it means the most derived class is responsible for creating virtual base class object (not must be direct base) and virtual base class object (not must be direct base) must be initialized only once.

    My understanding of the following statement correct?

    --------------------
    First, and only for the constructor of the most derived class as described below, virtual base classes shall
    be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph
    of base classes, where “left-to-right” is the order of appearance of the base class names in the derived
    class base-specifier-list.
    --------------------

    Quote Originally Posted by grumpy View Post
    Not true; a common purpose of software design is to achieve a set of observable effects in some defined order.

    In any event, George already quoted the relevant section of the standard.

    Constructor of the most derived class means the class that is being instantiated.

    regards,
    George

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Question answered.

    Quote Originally Posted by CornedBee View Post
    Yes.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-18-2009, 04:58 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. Not 100% sure when to use virtual methods in base class
    By Silvercord in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2003, 02:19 PM
  5. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM

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