By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

Virtual function from constructor

P: n/a
Jin

Does calling a virtual function from a constructor produce
undefined/implementation specific behavior or will the corresponding
base class function always get invoked?
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Fri, 19 Dec 2003 02:58:32 +0800, Jin <-> wrote:

Does calling a virtual function from a constructor produce
undefined/implementation specific behavior
Not by itself.

or will the corresponding base class function always get invoked?


No.

Check out the FAQ.

Jul 22 '05 #2

P: n/a
Alf P. Steinbach wrote:
Check out the FAQ.


Can you cite and quote the relevant FAQ?

Jul 22 '05 #3

P: n/a
On Thu, 18 Dec 2003 11:16:46 -0800, "E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
Alf P. Steinbach wrote:
Check out the FAQ.


Can you cite and quote the relevant FAQ?


Yes, I can.

And there are more than one that concerns this question.

But no, I don't intend to invest the time... ;-) It would be
service to the OP if you care to do that. Feel free.

Jul 22 '05 #4

P: n/a
Jin <-> wrote in message news:op**************@news.starhub.net.sg...

Does calling a virtual function from a constructor produce
undefined/implementation specific behavior or will the corresponding
base class function always get invoked?


A virtual function can be called from a constructor of a class, but not its
overrides of the said class (I remember the C++ FAQ has a section talking
about this). However, calling pure virtual function is undefined.
Jul 22 '05 #5

P: n/a
Jin
On Thu, 18 Dec 2003 18:58:46 GMT, Alf P. Steinbach <al***@start.no> wrote:
On Fri, 19 Dec 2003 02:58:32 +0800, Jin <-> wrote:

Does calling a virtual function from a constructor produce
undefined/implementation specific behavior


Not by itself.

or will the corresponding base class function always get invoked?


No.

Check out the FAQ.


What about explicitly invoking the base class function?

eg.
class Foo {
public:
Foo() { Foo::Func(); }
virtual void Funct() { /* do something */ }
}

Can member functions be accessed during construction for that matter?
Jul 22 '05 #6

P: n/a
On Fri, 19 Dec 2003 04:14:33 +0800, Jin <-> wrote:
What about explicitly invoking the base class function?
Yes, you can do that.

eg.
class Foo {
public:
Foo() { Foo::Func(); }
virtual void Funct() { /* do something */ }
}
But not that way, it would go like
struct Base
{
virtual void foo { ... }
};

struct Derived: Base
{
Derived()
{
Base::foo(); // Perhaps self-evident, calls Base::foo.
foo(); // Calls Derived::foo.
}

virtual void foo() { ... }
};

struct Derived2: Derived
{
virtual void foo() { ... }
};

Derived2 obj; // Derived constructor calls Base::foo
// and Derived::foo, not Derived2::foo.
Can member functions be accessed during construction for that matter?


Yes, but in the manner of porcupines making love: very carefully.

Jul 22 '05 #7

P: n/a
Jin
On Thu, 18 Dec 2003 20:15:26 GMT, Alf P. Steinbach <al***@start.no> wrote:
On Fri, 19 Dec 2003 04:14:33 +0800, Jin <-> wrote:
What about explicitly invoking the base class function?
Yes, you can do that.

eg.
class Foo {
public:
Foo() { Foo::Func(); }
virtual void Funct() { /* do something */ }
}


But not that way, it would go like


Does that mean the above example is not strictly conforming?
(quote chapter and verse, if possible)


struct Base
{
virtual void foo { ... }
};

struct Derived: Base
{
Derived()
{
Base::foo(); // Perhaps self-evident, calls Base::foo.
foo(); // Calls Derived::foo.
}
virtual void foo() { ... }
};

struct Derived2: Derived
{
virtual void foo() { ... }
};

Derived2 obj; // Derived constructor calls Base::foo
// and Derived::foo, not Derived2::foo.

Is this behavior guranteed by the Standard? ie. virtual function of the
most recently constructed base class in the hierachy gets invoked.

Can member functions be accessed during construction for that matter?


Yes, but in the manner of porcupines making love: very carefully.

Jul 22 '05 #8

P: n/a
On Fri, 19 Dec 2003 04:37:29 +0800, Jin <-> wrote:
On Thu, 18 Dec 2003 20:15:26 GMT, Alf P. Steinbach <al***@start.no> wrote:
On Fri, 19 Dec 2003 04:14:33 +0800, Jin <-> wrote:
What about explicitly invoking the base class function?
Yes, you can do that.

eg.
class Foo {
public:
Foo() { Foo::Func(); }
virtual void Funct() { /* do something */ }
}


But not that way, it would go like


Does that mean the above example is not strictly conforming?


No, it means there _is no base class_ in your example.

A "base class", in C++ terminology, is one that's derived from.
struct Base
{
virtual void foo { ... }
};

struct Derived: Base
{
Derived()
{
Base::foo(); // Perhaps self-evident, calls Base::foo.
foo(); // Calls Derived::foo.
}


virtual void foo() { ... }
};

struct Derived2: Derived
{
virtual void foo() { ... }
};

Derived2 obj; // Derived constructor calls Base::foo
// and Derived::foo, not Derived2::foo.


Is this behavior guranteed by the Standard?


Yes.

ie. virtual function of the
most recently constructed base class in the hierachy gets invoked.


No.

As I wrote earlier, see the C++ FAQ. Search Google for FAQ, C++,
Marshall Cline. Or find the monthly posting to this group.

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.