output of the program???

U

ujjwal bhoota

Hi,
Can any one explain me the behaviour of following program.




#include <iostream>

using namespace std;

class A {
public:
A() {
this->Foo();
}
virtual void Foo() {
cout << "A::Foo()" << endl;
}
};



class B : public A {
public:
B() {
this->Foo();
}
virtual void Foo() {
cout << "B::Foo()" << endl;
}
};






int main(int, char**)
{
B objectB;

return 0;
}



The output is

A::Foo()
B::Foo()



should not it be

B::Foo()
B::Foo()
as B::Foo() has been declared to be virtual


thanks and regards
UJJWAL
 
L

lilburne

ujjwal said:
The output is

A::Foo()
B::Foo()



should not it be

B::Foo()
B::Foo()
as B::Foo() has been declared to be virtual

B has not been constructed until after the A constructor has
finished, therefore there is no B for B::foo() to be called
whilst in A's constructor.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top