By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

Virtual function

P: n/a
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

Jun 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ju******@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
Jun 20 '06 #2

P: n/a
Thomas J. Gritzan wrote:
ju******@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

Jun 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.