Re: Virtual function
Thomas J. Gritzan wrote:
junw2000@gmail.com schrieb:
In the following code:
#include <iostream>
using namespace std;
class V {
public:
int i;
virtual void f() { cout << "V::f()" << endl;}
};
class A : virtual public V { //LINE1
void f() {cout << "A::f()" << endl; }
};
class B : virtual public V { //LINE2
void f() {cout << "B::f()" << endl;}
};
class D : public A, public B {}; //LINE3
int main() {}
Why LINE3 can not compile?
What about supplying your error message? I got this:
virt_inh.cpp:19: error: no unique final overrider for 'virtual void
V::f()' in 'D'
The error message applies to your LINE3. You have to overload the
function f() in class D, so that the compiler knows, what function it
should call when you do:
D* d = new D;
d->f(); // A::f or B::f?
Thomas
Hi,
Here is my understanding of implementation of Multiple Inheritance in
VC++
Let's consider three different cases,
Case I
class Base
{
public:
virtual void foo() { cout << "Base::foo()" << endl;}
};
class D1 : public Base
{
void foo() {cout << "D1::foo()" << endl; }
};
class D2 : public Base
{
void foo() {cout << "D2::foo()" << endl;}
};
class D1D2 : public D1, public D2
{
};
In this case, class D1D2 contains two vptrs, one corresponding to
vtable of D1 and another to that of D2
So even if you do not override foo() in D1D2, its ok because
D1* d1 = new D1D2 ;
d1->foo();
will use D1's vtable to call foo() and
D2* d2 = new D1D2 ;
d2->foo();
will use D2's vtable to call foo().
Case II
class Base
{
public:
int i ;
virtual void foo() { cout << "Base::foo()" << endl;}
Base():i(0){}
};
class D1 : virtual public Base
{
void foo() {cout << "D1::foo()" << endl; }
};
class D2 : virtual public Base
{
// foo not implemented
};
class D1D2 : public D1, public D2
{
};
In this case, because of virtual inheritance for object of calss D1D2,
compiler generates one vbptr for D1, one vbptr for D2 which points to
the offset of Base's subobject in D1D2 and only one vptr for calss D1D2
which points to a vtable containing B::foo() and issues warning that
B::foo() dominates A::foo(). So
D2* d2 = new D1D2 ;
d2->foo() ;
will call D1::foo() only.
Case III
class Base
{
public:
int i ;
virtual void foo() { cout << "Base::foo()" << endl;}
Base():i(0){}
};
class D1 : virtual public Base
{
void foo() {cout << "D1::foo()" << endl; }
};
class D2 : virtual public Base
{
void foo() {cout << "D2::foo()" << endl;}
};
class D1D2 : public D1, public D2
{
};
In this case since there is going to be only one vtable for D1D2, and
both D1 and D2 override foo(),compiler finds itself unable to figure
out what to be put in vtable ( D1::foo() or D2::foo() ) and hence flags
an error for ambiguous inheritance.
Regards,
Uday Bidkar