What will the following simple program output to screen?

 
#include <iostream>
using namespace std;
 
struct A 
{
    virtual void Foo (int n = 10) {
        cout << "A::Foo, n = " << n << endl;
    }
};
 
struct B : A 
{
    virtual void Foo (int n = 20) {
        cout << "B::Foo, n = " << n << endl;
    }
};
 
int main() {
    A * pa = new B ();
    pa->Foo ();
 
    return 0;
}
PS: for the explanation of this behavior see §8.3.6 p.10 in C++ language standard (e.g. here).
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s