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

Calling virtual function in constructor

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a

"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

P: n/a
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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.