Virtual function

J

junw2000

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?


When I remove the "virtual" at LINE1 and LINE2 and get the following
code:

#include <iostream>

using namespace std;

class V {
public:
int i;
virtual void f() { cout << "V::f()" << endl;}
};

class A : public V { //LINE1
void f() {cout << "A::f()" << endl; }
};

class B : public V { //LINE2
void f() {cout << "B::f()" << endl;}
};

class D : public A, public B {}; //LINE3

int main() {}


LINE3 can be compiled. What is the difference between the two cases?

Thank you.

Jack
 
T

Thomas J. Gritzan

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
 
U

Uday Bidkar

Thomas said:
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
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top