Topic: an issue in constructor order dependency


Author: kak@purdue.edu (Avi Kak)
Date: Wed, 6 Nov 2002 00:55:10 +0000 (UTC)
Raw View
Hello:

  I am trying to figure out what the standard has to say
  about the following issue related to "Constructor Order
  Dependency," but am not having any luck.   I think that
  Clause 12.6.2 covers the situation I describe below,
  but I am not sure. If someone could shed further light
  on the subject, I'd be grateful.

  Let's say a base-class constructor calls a function
  foo() that is defined as virtual in the base class and
  that has an override definition in a derived class.

  Let's further say that a derived-class constructor
  invokes the above-mentioned base-class constructor.
  The question now is as to which foo() will be used in
  the construction of a derived-class object.

  The g++ compilers I have tested invoke the base-class
  foo(), but a similar program in Java invokes the
  derived-class foo().  So I am trying to figure out as
  to which clause in the standard dictates this behavior
  of C++.

  Here is the program I used for testing this order
  dependency:

    ---------------------------------------------------
    #include <iostream>
    using namespace std;

    class Base {
    public:
        Base() { foo(); }
        virtual void foo() {
            cout << "Base's foo invoked" << endl;
        }
    };

    class Derived : public Base {
    public:
        Derived() {}
        void foo() {
            cout << "Derived's foo invoked" << endl;
        }
    };

    int main() {
        Derived d;                //invokes Base's foo()
        return 0;
    }
    --------------------------------------------------


  Avi Kak
  kak@purdue.edu

  P.S.:  I would like to thank Markus Mauhart, Victor
         Bazarov, and Martin Lowis for responding to my
         previous plea for help.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dontspamme@spam.you (Ray Lischner)
Date: Wed, 6 Nov 2002 01:16:35 +0000 (UTC)
Raw View
On Tuesday 05 November 2002 04:55 pm, Avi Kak wrote:

>   Let's say a base-class constructor calls a function
>   foo() that is defined as virtual in the base class and
>   that has an override definition in a derived class.
>
>   Let's further say that a derived-class constructor
>   invokes the above-mentioned base-class constructor.
>   The question now is as to which foo() will be used in
>   the construction of a derived-class object.
>
>   The g++ compilers I have tested invoke the base-class
>   foo()... So I am trying to figure out as
>   to which clause in the standard dictates this behavior
>   of C++.

See section 12.7, par. 3 to understand why g++ is correct.
--
Ray Lischner, author of C++ in a Nutshell (forthcoming)
http://www.tempest-sw.com/cpp/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net ("James Kuyper Jr.")
Date: Wed, 6 Nov 2002 03:08:48 +0000 (UTC)
Raw View
Avi Kak wrote:
 > Hello:
 >
 >   I am trying to figure out what the standard has to say
 >   about the following issue related to "Constructor Order
 >   Dependency," but am not having any luck.   I think that
 >   Clause 12.6.2 covers the situation I describe below,
 >   but I am not sure. If someone could shed further light
 >   on the subject, I'd be grateful.
 >
 >   Let's say a base-class constructor calls a function
 >   foo() that is defined as virtual in the base class and
 >   that has an override definition in a derived class.
 >
 >   Let's further say that a derived-class constructor
 >   invokes the above-mentioned base-class constructor.
 >   The question now is as to which foo() will be used in
 >   the construction of a derived-class object.

The base class constructor is called before the derived-class
constructor, not invoked by it. The key point is that objects change
their type as each constructor is successively invoked.
At the time the base-class constructor is called, the object IS of the
base class type. Therefore it's the base-class version of foo() that is
called.
By the time foo() is called in the derived-class constructor, the object
is now of the derived class type. Therefore, it's the derived class
version which is called.

The relevant section is 12.7p3: "When a virtual function is called
directly or indirectly from a constructor (including from the
_mem-initializer_ for a data member) or from a destructor, and the
object to which the call applies is the object under construction or
destruction, the function call is the one defined in the constructor or
destructor's own class or in one of its bases, but not a function
overriding it in a class derived from the constructor or destructor's
class, or overriding it in one of the other base classes of the most
derived object."

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]