473,407 Members | 2,306 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,407 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 3005
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.