Topic: Virtual calls in constructors does not work
Author: Gerard Weatherby <gerardw@alum.mit.edu>
Date: 1997/04/21 Raw View
Jeffrey C. Dege wrote:
>
> Not that this in any way contradicts what you are saying, but you _can_
> provide an implementation for a pure virtual function. It can only be
> called via subclasses, and then only with an explicit scope, but it can
> be a useful technique. So there actually _might_ be such a function,
> but you can't call it.
I couldn't find anywhere in the draft that prohibits a class from
calling it's own pure virtual function with explicit scope:
#include <iostream.h>
struct Foo {
Foo( );
virtual void fooit() = 0;
};
void Foo::fooit() { cout << "Foo::fooit!" << endl; }
Foo::Foo( )
{ Foo::fooit( ); }
struct Bar : public Foo {
virtual void fooit( ) {};
};
int main() {
Bar bar;
}
---
Not to say that is makes any sense -- just that's it's allowable. (I
think.)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: paul@ra.avid.com (Paul Miller)
Date: 1997/04/19 Raw View
dat93ser@ludat.lth.se (Stefan Eriksson) writes:
>The constructor doeas not call overridden methods, even if they are virtual.
>Will this cause an error if the method is pure virtual?
Yes, since there is no function "yet" which can be called. When you're in a
constructor, the object that you're constructing has not been "created" yet.
This includes VTABLE setup. But it's also more of a logistical thing - since
constructors are called in order from base class up, attempting to call a
virtual function for a derived class which has not yet been created is not
possible.
--
Paul Miller | paul@elastic.avid.com
SGI Software Engineer | Opinions expressed here are my own.
Elastic Reality - a division of Avid Technology, Inc.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jdege@jdege.visi.com (Jeffrey C. Dege)
Date: 1997/04/21 Raw View
On 19 Apr 97 15:26:33 GMT, Paul Miller <paul@ra.avid.com> wrote:
>
>dat93ser@ludat.lth.se (Stefan Eriksson) writes:
>
>>The constructor doeas not call overridden methods, even if they are virtual.
>>Will this cause an error if the method is pure virtual?
>
>Yes, since there is no function "yet" which can be called. When you're in a
>constructor, the object that you're constructing has not been "created" yet.
>This includes VTABLE setup. But it's also more of a logistical thing - since
>constructors are called in order from base class up, attempting to call a
>virtual function for a derived class which has not yet been created is not
>possible.
Not that this in any way contradicts what you are saying, but you _can_
provide an implementation for a pure virtual function. It can only be
called via subclasses, and then only with an explicit scope, but it can
be a useful technique. So there actually _might_ be such a function,
but you can't call it.
#include <iostream.h>
struct Foo {
virtual void fooit() = 0;
};
void Foo::fooit() { cout << "Foo::fooit!" << endl; }
struct Bar : public Foo {
virtual void fooit() { Foo::fooit(); }
};
int main() {
Bar bar; bar.fooit();
}
--
Personally, I think my choice in the mostest-superlative-computer wars has to
be the HP-48 series of calculators. They'll run almost anything. And if they
can't, while I'll just plug a Linux box into the serial port and load up the
HP-48 VT-100 emulator.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Paul D. DeRocco" <strip_these_words_pderocco@ix.netcom.com>
Date: 1997/04/12 Raw View
Paul Black wrote:
>
> The actual method called in the constructor is the one in the most derived
> class that is no more "derived" than the class being constructed.
> This means that the method is either in the current class or one of the
> base classes (see draft section 12.7). The behaviour you posted is correct.
Doesn't this represent a change from the past? I recall reading
somewhere that if D is derived from B, and you construct a D, the object
doesn't officially become a B until B::B() is finished, and it doesn't
become a D until D::D() is finished, meaning that a call to virtual f()
inside D::D() will invoke B::f(), not D::f(). I always thought that
stank, and am thrilled if they've changed it, or if I was mistaken in
the first place.
--
Ciao,
Paul D. DeRocco
(Please send e-mail to mail:pderocco@ix.netcom.com instead of the
return address, which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/14 Raw View
"Paul D. DeRocco" <strip_these_words_pderocco@ix.netcom.com> writes:
>Paul Black wrote:
>>
>> The actual method called in the constructor is the one in the most derived
>> class that is no more "derived" than the class being constructed.
>> This means that the method is either in the current class or one of the
>> base classes (see draft section 12.7). The behaviour you posted is correct.
>
>Doesn't this represent a change from the past?
No (at least I don't think so).
>I recall reading
>somewhere that if D is derived from B, and you construct a D, the object
>doesn't officially become a B until B::B() is finished, and it doesn't
>become a D until D::D() is finished,
Correct.
>meaning that a call to virtual f()
>inside D::D() will invoke B::f(), not D::f().
Nope.
An object declared to be of type `D' is not officially a `D' until
after the D constructor has completed, but its polymorphic behaviour is
as an object of type `D' as soon as the base class initializers (if
any) have completed.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: John David Galt <jdg@but-i-dont-like-spam.boxmail.com>
Date: 1997/04/14 Raw View
> Hi. The basic question is the following: I declare a base class and a
> derived class. The base class constructor is called, and within this
> constructor the virtual method is called. According to what I know, the
> actual method called from the constructor should be the derived
> method. All books I have do not say anything on the contrary. But it
When you call a virtual function, the actual function to be called is decided
at run time according to the type of object it is called from.
But within the base class constructor, there is no derived class object, even
if you are in the process of creating one. When you declare an object of
class Derived, these steps occur:
1. The implementation obtains a block of memory big enough to hold a Derived
object. This is not yet an object, it is uninitialized memory.
2. Base::Base() is called, with "this" set to the address within the block
where the Base sub-object will go. When the Base::Base() function body
begins executing, that part of the block is considered to be a Base
object, rather than uninitialized memory; however, no Derived object
exists yet in any sense. So if Base::Base() calls a virtual function VF,
Base::VF() must be called.
3. Derived::Derived() is called, with "this" set to the address of the block
obtained in step 1. When the Derived::Derived() function body begins
executing, _then_ the block is considered to be a Derived object.
The same holds true for destructors. Once Derived::~Derived() finishes
executing, the Derived object no longer exists. So if Base::~Base() calls
VF, it will use Base::VF().
John David Galt
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Cesar Crusius <crusius@coffee.stanford.edu>
Date: 1997/04/08 Raw View
Hi. The basic question is the following: I declare a base class and a
derived class, with a virtual method. Now I declare an instance of the
derived class. The base class constructor is called, and within this
constructor the virtual method is called. According to what I know, the
actual method called from the constructor should be the derived
method. All books I have do not say anything on the contrary. But it
happens I have the base constructor called. Since this is not mentioned in
any book I have, should I consider a compiler bug, or this is the expected
behavior? If it is expected, where did you find it? Attached is a sample
program, I compiled it using g++ 2.7.2, and here's the output:
Base constructor.
Base method.
Derived constructor.
Now in main.
Derived method.
Derived destructor.
Base destructor.
Here's the program. Hope you can solve the puzzle. I am totally lost,
since this behavior is against all I expected. Note that the call from
main (ie, a call outside the constructor) works as expected. Regards,
Cesar Crusius
crusius@leland.stanford.edu
----------------------------------------------------------------------
#include <iostream.h>
class base
{
public:
base();
virtual ~base();
virtual void method(void);
};
class derived:public base
{
public:
derived();
virtual ~derived();
virtual void method(void);
};
base *global;
base::base(void)
{
// atualize global pointer "global"
global=this;
cout << "Base constructor.\n";
// I want the derived method to be called. Won't happen
method();
}
base::~base() { cout << "Base destructor.\n"; }
derived::derived(void) { cout << "Derived constructor.\n"; }
derived::~derived() { cout << "Derived destructor.\n"; }
void base::method(void) { cout << "Base method.\n"; }
void derived::method(void) { cout << "Derived method.\n"; }
// Declare the derived instance. Will call base constructor.
derived Instance;
void main(void)
{
cout << "Now in main.\n";
// Now it will work...
global->method();
}
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: dat93ser@ludat.lth.se (Stefan Eriksson)
Date: 1997/04/08 Raw View
On 08 Apr 97 06:13:40 GMT, Cesar Crusius <crusius@coffee.stanford.edu> wrote:
>Hi. The basic question is the following: I declare a base class and a
>derived class, with a virtual method. Now I declare an instance of the
>derived class. The base class constructor is called, and within this
>constructor the virtual method is called. According to what I know, the
>actual method called from the constructor should be the derived
>method. All books I have do not say anything on the contrary. But it
>happens I have the base constructor called. Since this is not mentioned in
>any book I have, should I consider a compiler bug, or this is the expected
>behavior? If it is expected, where did you find it? Attached is a sample
>program, I compiled it using g++ 2.7.2, and here's the output:
>
>Base constructor.
>Base method.
>Derived constructor.
>Now in main.
>Derived method.
>Derived destructor.
>Base destructor.
I found the rule in the standard. It is 12.7.3, and here it is:
3 Member functions, including virtual functions (_class.virtual_), can
be called during construction or destruction (_class.base.init_).
[--Begin interesting part]
When a virtual function is called directly or indirectly from a con-
structor (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 called 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 (_intro.object_). If
[--End interesting part]
the virtual function call uses an explicit class member access
(_expr.ref_) and the object-expression refers to the object under con-
struction or destruction but its type is neither the constructor or
destructor's own class or one of its bases, the result of the call is
undefined. [Example:
class V {
public:
virtual void f();
virtual void g();
};
class A : public virtual V {
public:
virtual void f();
};
class B : public virtual V {
public:
virtual void g();
B(V*, A*);
};
class D : public A, B {
public:
virtual void f();
virtual void g();
D() : B((A*)this, this) { }
};
B::B(V* v, A* a) {
f(); // calls V::f, not A::f
g(); // calls B::g, not D::g
v->g(); // v is base of B, the call is well-defined, calls B::g
a->f(); // undefined behavior, a's type not a base of B
}
--end example]
The constructor doeas not call overridden methods, even if they are virtual.
Will this cause an error if the method is pure virtual?
//Stefan
-- ___________________________________________________________________
__w__ Stefan Eriksson kamnarsvagen 13:C108 226 46 LUND, tel: 046-393668
o.o Mail to dat93ser@ludat.lth.se, stefer@df.lth.se
_m__~__m__HomePage_________http://www.ludat.lth.se/~dat93ser_________________
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jim.hyslop@leitch.com (Jim Hyslop)
Date: 1997/04/08 Raw View
In article <tlcd8s6kuae.fsf@coffee.Stanford.EDU>,
crusius@coffee.stanford.edu says...
> Hi. The basic question is the following: I declare a base class and a
> derived class, with a virtual method. Now I declare an instance of the
> derived class. The base class constructor is called, and within this
> constructor the virtual method is called.
OK, I'm with you so far.
> According to what I know, the
> actual method called from the constructor should be the derived
> method.
Oops, here's where you're going astray.
> All books I have do not say anything on the contrary.
Re-read the sections on constructors and the order in which objects
are constructed, including virtual function tables. If you still
don't find it in your books, then use the books for what they were
intended (campfire starters) and get new books. :-)
> But it
> happens I have the base constructor called. Since this is not mentioned in
> any book I have, should I consider a compiler bug, or this is the expected
> behavior?
No, this is expected behaviour. You're thinking that all the virtual
handlers are set up first, then the constructors are called. What
really happens is that the base class is *completely* constructed,
including the virtual function handlers, *then* the derived class is
constructed and its virtual functions set up.
> If it is expected, where did you find it?
CD2, section 12.7.3, Construction and destruction [class.cdtor]:
"Member functions, including virtual functions (10.3), can be called
during construction or destruction (12.6.2). 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 called 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 (1.7)."
[sample code and output snipped]
--
Jim Hyslop
jim.hyslop@leitch.com
Children have an innate understanding of statistics. When they report
"Everyone is doing it," their findings are usually based on a survey
of one.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: herbs@cntc.com (Herb Sutter)
Date: 1997/04/08 Raw View
Cesar Crusius <crusius@coffee.stanford.edu> wrote:
>Hi. The basic question is the following: I declare a base class and a
>derived class, with a virtual method. Now I declare an instance of the
>derived class. The base class constructor is called, and within this
>constructor the virtual method is called. According to what I know, the
>actual method called from the constructor should be the derived
>method. All books I have do not say anything on the contrary.
See Stroustrup's C++PL2 page 582. The basic idea is that virtual functions do
not behave (completely) virtually in ctors and dtors. When a virtual function
is called from a class B's ctor or dtor, the actual function called must be in
B or one of its bases... this is because any further-derived parts of the
object have not been constructed yet or have already been destroyed.
For instance, say you have class X a base of class Y a base of class Z, where
X declares a virtual function f() which both Y and Z override. Say Y's ctor
calls f(). Then even if you create a Z object, when Y's ctor executes it will
call Y::f()... because during Y's ctor the type of the object is considered to
be a Y (even though we're about to go on to construct the Z part of the
object, but that doesn't exist yet and so it would be Very Bad to call Z
member functions) and X::f() is virtual, hence you get Y::f() called, not
X::f() or Z::f().
To quote from CD2 12.7/3:
3 Member functions, including virtual functions (_class.virtual_), can
be called during construction or destruction (_class.base.init_).
When a virtual function is called directly or indirectly from a con-
structor (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 called 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 (_intro.object_). If
the virtual function call uses an explicit class member access
(_expr.ref_) and the object-expression refers to the object under con-
struction or destruction but its type is neither the constructor or
destructor's own class or one of its bases, the result of the call is
undefined. [Example:
class V {
public:
virtual void f();
virtual void g();
};
class A : public virtual V {
public:
virtual void f();
};
class B : public virtual V {
public:
virtual void g();
B(V*, A*);
};
class D : public A, B {
public:
virtual void f();
virtual void g();
D() : B((A*)this, this) { }
};
B::B(V* v, A* a) {
f(); // calls V::f, not A::f
g(); // calls B::g, not D::g
v->g(); // v is base of B, the call is well-defined, calls
B::g
a->f(); // undefined behavior, a's type not a base of B
}
--end example]
---
Herb Sutter (mailto:herbs@cntc.com)
Current Network Technologies Corp. (http://www.cntc.com)
2695 North Sheridan Way, Suite 150, Mississauga ON Canada L5K 2N6
Tel 416-805-9088 Fax 905-822-3824
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: dat93ser@ludat.lth.se (Stefan Eriksson)
Date: 1997/04/08 Raw View
On 08 Apr 97 06:13:40 GMT, Cesar Crusius <crusius@coffee.stanford.edu> wrote:
>Hi. The basic question is the following: I declare a base class and a
>derived class, with a virtual method. Now I declare an instance of the
>derived class. The base class constructor is called, and within this
>constructor the virtual method is called. According to what I know, the
>actual method called from the constructor should be the derived
>method. All books I have do not say anything on the contrary. But it
>happens I have the base constructor called. Since this is not mentioned in
>any book I have, should I consider a compiler bug, or this is the expected
>behavior? If it is expected, where did you find it? Attached is a sample
>program, I compiled it using g++ 2.7.2, and here's the output:
>
>Base constructor.
>Base method.
>Derived constructor.
>Now in main.
>Derived method.
>Derived destructor.
>Base destructor.
>
>Here's the program. Hope you can solve the puzzle. I am totally lost,
>since this behavior is against all I expected. Note that the call from
>main (ie, a call outside the constructor) works as expected. Regards,
As far as I know, calls to methods from the constructors does not call the
overridden method, even if it is virtual.
My argument for this is that the base class constructor is run before the
derived class constructor during the initialization, and herefore, the
"derived part" of the object is not initilized and one should not be able
to manipulate that part of the object.
Not a very technical description of the problem, but I hope you understand.
I tried to find this in the standard document on the web, but had no success.
Could someone please point out the rule(s) that applies to this problem?
The standard can be found at:
http://www.maths.warwick.ac.uk/c++/pub/wp/html/cd2/
//Stefan
-- ___________________________________________________________________
__w__ Stefan Eriksson kamnarsvagen 13:C108 226 46 LUND, tel: 046-393668
o.o Mail to dat93ser@ludat.lth.se, stefer@df.lth.se
_m__~__m__HomePage_________http://www.ludat.lth.se/~dat93ser_________________
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/04/08 Raw View
Cesar Crusius <crusius@coffee.stanford.edu> wrote:
: Hi. The basic question is the following: I declare a base class and a
: derived class, with a virtual method. Now I declare an instance of the
: derived class. The base class constructor is called, and within this
: constructor the virtual method is called. According to what I know, the
: actual method called from the constructor should be the derived
: method.
No, it should call base method. During the construction of the
Base, the class is not Derived yet. Therefore Base method will be
called.
If you want to play with it further, you can check and see that
after Derived destructor finished, and Base destructor is called,
the object is not Derived any more, amd again, base methods will
be called.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/04/08 Raw View
Stefan Eriksson <dat93ser@ludat.lth.se> wrote:
: The constructor doeas not call overridden methods, even if they are virtual.
: Will this cause an error if the method is pure virtual?
Pure virtual methods are allowed to have a definition, for cases like
this, for example. Pure virtual simply means that a compiler will
enforce that derived methods must override it. Moreover, if your
destructor is pure virtual, then you absolutely _must_ define it in
the base class as well. Because during the destruction of the derived
class, you'll get to the point when a base class has to be destroyed,
but it is not derived any more, so a base class destructor
(pure virtual!) will be called.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul Black" <paul.black@vf.vodafone.co.uk>
Date: 1997/04/09 Raw View
Cesar Crusius <crusius@coffee.stanford.edu>
> Hi. The basic question is the following: I declare a base class and a
> derived class, with a virtual method. Now I declare an instance of the
> derived class. The base class constructor is called, and within this
> constructor the virtual method is called. According to what I know, the
> actual method called from the constructor should be the derived
> method. All books I have do not say anything on the contrary. But it
> happens I have the base constructor called. Since this is not mentioned in
[code and other stuff snipped]
The actual method called in the constructor is the one in the most derived
class that is no more "derived" than the class being constructed.
This means that the method is either in the current class or one of the
base classes (see draft section 12.7). The behaviour you posted is correct.
Paul
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jbuck@Synopsys.COM (Joe Buck)
Date: 1997/04/09 Raw View
dat93ser@ludat.lth.se (Stefan Eriksson) writes:
>The constructor doeas not call overridden methods, even if they are virtual.
>Will this cause an error if the method is pure virtual?
Yes. As an example of what this means to implementers, gcc/g++ generates
code to handle that case, printing a message "pure virtual function
called" to cerr and aborting the program.
--
-- Joe Buck http://www.synopsys.com/pubs/research/people/jbuck.html
Help stamp out Internet spam: see http://www.vix.com/spam/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/09 Raw View
Cesar Crusius <crusius@coffee.stanford.edu> writes:
>Hi. The basic question is the following: I declare a base class and a
>derived class, with a virtual method. Now I declare an instance of the
>derived class. The base class constructor is called, and within this
>constructor the virtual method is called. According to what I know, the
>actual method called from the constructor should be the derived
>method. All books I have do not say anything on the contrary. But it
>happens I have the base constructor called. Since this is not mentioned in
>any book I have, should I consider a compiler bug, or this is the expected
>behavior?
It is not a compiler bug; this behaviour is the behaviour specified in
the ARM and the C++ draft working paper.
>If it is expected, where did you find it?
See 12.7[class.cdtor]/3 in the C++ DWP.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/04/09 Raw View
dat93ser@ludat.lth.se (Stefan Eriksson) writes:
|> On 08 Apr 97 06:13:40 GMT, Cesar Crusius <crusius@coffee.stanford.edu> wrote:
|> >Hi. The basic question is the following: I declare a base class and a
|> >derived class, with a virtual method. Now I declare an instance of the
|> >derived class. The base class constructor is called, and within this
|> >constructor the virtual method is called. According to what I know, the
|> >actual method called from the constructor should be the derived
|> >method. All books I have do not say anything on the contrary. But it
|> >happens I have the base constructor called. Since this is not mentioned in
|> >any book I have, should I consider a compiler bug, or this is the expected
|> >behavior? If it is expected, where did you find it? Attached is a sample
|> >program, I compiled it using g++ 2.7.2, and here's the output:
|> [--Begin interesting part]
|>
|> When a virtual function is called directly or indirectly from a con-
|> structor (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 called 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 (_intro.object_). If
|>
|> [--End interesting part]
If you think about it, this is very logical: during construction, the
object doesn't have the type of the derived class, and the function
called should correspond to the dynamic type of the object. (You might
want to look at the behavior of RTTI in such cases, as well. A dynamic
cast to the derived type from the constructor of the base type will also
fail.)
|> The constructor doeas not call overridden methods, even if they are virtual.
|> Will this cause an error if the method is pure virtual?
Undefined behavior; most of the implementations I've seen abort.
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]