473,410 Members | 1,914 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,410 software developers and data experts.

Calling virtual function in constructor

Microsoft C++ Version 13.10.3077

Why is virtual function init() called in constructor here?

====== foo.cpp ======
#include "iostream"
using namespace std;

class Base
{
public:
virtual void init () {cout << "Base::init()" << endl; }
Base() { cout << "Base::Ctor()" << endl;}
};

class Derived : public Base
{
public:
void foo() { cout << "Derived::foo()" << endl; }
virtual void init () { cout << "Derived::init()" << endl; foo ();
}
Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
};

int main ()
{
Base * pb = new Derived;
return 0;
}

=====================
====== Run ======

Base::Ctor()
Derived::Ctor()
Derived::init() // Why not Base::init()
Derived::foo()

================
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Feb 16 '06 #1
8 2662

"Alex Vinokur" <al****@users.sourceforge.net> wrote in message
| Microsoft C++ Version 13.10.3077
|
| Why is virtual function init() called in constructor here?

You can call virtual functions inside constructor, but the call is resolved
statically.

[snip]

| ====== Run ======
|
| Base::Ctor()
| Derived::Ctor()
| Derived::init() // Why not Base::init()

Why should it be Base::init ?

| Derived::foo()

Sharad
Feb 16 '06 #2
Alex Vinokur wrote:
Microsoft C++ Version 13.10.3077

Why is virtual function init() called in constructor here?
Why not?
====== foo.cpp ======
#include "iostream"
Should be:

#include <iostream>
using namespace std;

class Base
{
public:
virtual void init () {cout << "Base::init()" << endl; }
Base() { cout << "Base::Ctor()" << endl;}
};

class Derived : public Base
{
public:
void foo() { cout << "Derived::foo()" << endl; }
virtual void init () { cout << "Derived::init()" << endl; foo ();
}
Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
};

int main ()
{
Base * pb = new Derived;
return 0;
}

=====================
====== Run ======

Base::Ctor()
Derived::Ctor()
Derived::init() // Why not Base::init()
Because Derived::init() overrides it.
Derived::foo()


Feb 16 '06 #3

Rolf Magnus wrote:
Alex Vinokur wrote: [snip]

Why is virtual function init() called in constructor here?


Should be:

Why is virtual function Derived::init() (not Base::init()) called in
constructor here?

Why not?
====== foo.cpp ======
#include "iostream"


Should be:

#include <iostream>

Of course. Thanks.

[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Feb 16 '06 #4

Sharad Kala wrote:
"Alex Vinokur" <al****@users.sourceforge.net> wrote in message
| Microsoft C++ Version 13.10.3077
|
| Why is virtual function init() called in constructor here?

You can call virtual functions inside constructor, but the call is resolved
statically.
When can it cause problem?

The program I sent is not an example of such a problem (!?)

[snip]

| ====== Run ======
|
| Base::Ctor()
| Derived::Ctor()
| Derived::init() // Why not Base::init()

Why should it be Base::init ?

| Derived::foo()

Sharad


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Feb 16 '06 #5
Sharad Kala wrote:
| Why is virtual function init() called in constructor here?

You can call virtual functions inside constructor, but the call is
resolved statically.


That's actually not true. Well, if you call it directly from the
constructor, the effect is more or less the same. But if you go indirect
(like through another function call), you can see that polymorphism is
already active in the constructor. You just have to remember that during
exeuction of the derived class's constructor, the object has obviously only
been constructed up to that class. Therefore its dynamic type is
temporarily that class. See this example:

#include <iostream>

class Base
{
public:
void init()
{
do_init();
}
virtual void do_init()
{
std::cout << "Base::do_init()\n";
}
};

class Derived : public Base
{
public:
Derived() { init(); }

void do_init()
{
std::cout << "Derived::do_init()\n";
}
};

class EvenMoreDerived : public Derived
{
public:
void do_init()
{
std::cout << "EvenMoreDerived::do_init()\n";
}
};

int main()
{
EvenMoreDerived();
}

Since init() is called from Derived's constructor, Derived's implementation
of do_init() gets called, even though init() is a member function of Base.

Feb 16 '06 #6
Alex Vinokur wrote:
> Why is virtual function init() called in constructor here?


Should be:

Why is virtual function Derived::init() (not Base::init()) called in
constructor here?


As I wrote, because Derived::init() overrides Base::init().

Feb 16 '06 #7

"Rolf Magnus" <ra******@t-online.de> wrote in message | Sharad Kala wrote:
|
| > | Why is virtual function init() called in constructor here?
| >
| > You can call virtual functions inside constructor, but the call is
| > resolved statically.
|
| That's actually not true. Well, if you call it directly from the
| constructor, the effect is more or less the same. But if you go indirect
| (like through another function call), you can see that polymorphism is
| already active in the constructor.

Oh yeah..thanks for pointing it out.

Sharad
Feb 16 '06 #8

"Alex Vinokur" <al****@users.sourceforge.net> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com...

[snip]

====== foo.cpp ======
#include "iostream"
using namespace std;

class Base
{
public:
virtual void init () {cout << "Base::init()" << endl; }
Base() { cout << "Base::Ctor()" << endl;}
};

class Derived : public Base
{
public:
void foo() { cout << "Derived::foo()" << endl; }
virtual void init () { cout << "Derived::init()" << endl; foo ();
}
Derived () : Base() { cout << "Derived::Ctor()" << endl; init ();}
};

int main ()
{
Base * pb = new Derived;
return 0;
}

=====================
====== Run ======

Base::Ctor()
Derived::Ctor()
Derived::init() // Why not Base::init()
Derived::foo()

=================


[snip]

The example above doesn't depict the problem.

Here is an example that depicts the problem and contains an answer.

====== foo2.cpp ======
#include "iostream"
using namespace std;

class Base
{
public:
virtual void init ()
{
cout << "Base::init()" << endl;
}
Base()
{
cout << "Base::ctor()" << endl;
init (); // only Base::init() is called
}
void foo()
{
cout << "Base::foo()" << endl;
init (); // virtual init() of a _relevant_ class is called, i.e., Base::init() or Derived::::init()
}
};
class Derived : public Base
{
public:
void init ()
{
cout << "Derived::init()" << endl;
}
Derived () : Base()
{
cout << "Derived::ctor()" << endl;
}
};
int main ()
{
Base * pb = new Derived;

cout << endl;
pb->foo();

return 0;

}
======================
====== Run ======

Base::ctor()
Base::init() // Base::ctor() calls Base::init() because Derived-object doesn't exist here
Derived::ctor()

Base::foo()
Derived::init() // Base::foo() calls Derived::init() because Derived-object exists here

=================
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Feb 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
6
by: BigMan | last post by:
Is it safe to call nonvirtual member functions from ctors and dtors? What about virtual ones?
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
1
by: JP | last post by:
Hi, I am facing a strange problem, please take a look at the code below: public SaveTree() { ........ ...... ......//some code Initranges()
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...
3
by: Biswajit Jena | last post by:
Is it possible to call a member function in constructor of the same class ? how ? I have a templated member function which I want to call in the constructor of the same class. How will I do...
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;"...
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.