Calling virtual function in constructor

A

Alex Vinokur

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
 
S

Sharad Kala

| 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
 
R

Rolf Magnus

Alex said:
Microsoft C++ Version 13.10.3077

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

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

Should be:

#include said:
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.
 
A

Alex Vinokur

Sharad said:
| 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
 
R

Rolf Magnus

Sharad said:
| 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.
 
R

Rolf Magnus

Alex said:
Should be:

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

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

Sharad Kala

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

Alex Vinokur

[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

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

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,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top