473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Virtual function from constructor

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
8 1048
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
Alf P. Steinbach wrote:
Check out the FAQ.


Can you cite and quote the relevant FAQ?

Jul 22 '05 #3
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas Matthews | last post by:
Hi, Is placing the keyword "virtual" in front of a constructor allowed as in the sample below? class TTable { virtual TTable(); }; My compiler, Borland Builder 5.2, has system libraries
18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
10
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that class A have some derived classes. I want A's...
7
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.