J
junw2000
In the following code:
#include <iostream>
using namespace std;
class V {
private:
int i;
public:
virtual void f() { cout << "V::f()" << endl;}
};
class A : public V {
void f() {cout << "A::f()" << endl; }
};
class B : public V {
void f() {cout << "B::f()" << endl;} //LINE0
};
class D : public A, public B { };
int main() {
D d; //LINE1
B* bp = &d;
V* vptr = bp;
vptr->f(); //LINE2
}
At LINE1, the object d has two copies of i, one is A::i, one is B::i,
right?
How about f(), does the object d also have two copies of f(), one is
A::f(), one is B::f()?
The output of LINE2 is "B::f()", which means LINE0 is executed. B::f()
is private, why B::f() can be executed in main()?
Thanks a lot.
Jack
#include <iostream>
using namespace std;
class V {
private:
int i;
public:
virtual void f() { cout << "V::f()" << endl;}
};
class A : public V {
void f() {cout << "A::f()" << endl; }
};
class B : public V {
void f() {cout << "B::f()" << endl;} //LINE0
};
class D : public A, public B { };
int main() {
D d; //LINE1
B* bp = &d;
V* vptr = bp;
vptr->f(); //LINE2
}
At LINE1, the object d has two copies of i, one is A::i, one is B::i,
right?
How about f(), does the object d also have two copies of f(), one is
A::f(), one is B::f()?
The output of LINE2 is "B::f()", which means LINE0 is executed. B::f()
is private, why B::f() can be executed in main()?
Thanks a lot.
Jack