473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Virtual function

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
2 3001
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.