Virtual function from constructor

J

Jin

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

Alf P. Steinbach

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.
 
D

Deming He

Jin said:
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.
 
J

Jin

Not by itself.



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?
 
A

Alf P. Steinbach

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.
 
J

Jin

Yes, you can do that.



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.
 
A

Alf P. Steinbach

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.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top