473,396 Members | 1,914 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,396 software developers and data experts.

Overiding a virtual function of multiple inheritance base class

Please look at the code below,

class BaseCol
{
public:
virtual void a() { cout << "BaseCol::a" << endl; }
virtual void b() { cout << "BaseCol::b" << endl; }
};

class ChildCol1 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol1::a" << endl; }
virtual void b() { cout << "ChildCol1::b" << endl; }
};

class ChildCol2 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol2::a" << endl; }
virtual void b() { cout << "ChildCol2::b" << endl; }
};

class Row : public ChildCol1, public ChildCol2
{
public:
// i want to overide the b function of ChildCol1 only !!
void b() { cout << "Row1::b" << endl; }
};

Now,
In Row class, How do i tell the compiler that i want to overide for
example only the b() function of ChildCol1 base class (and leave the
implementation for this function from ChildCol2) ?

TNX,
Ronnie
Jul 22 '05 #1
4 4724
Ronnie wrote:
Please look at the code below,

class BaseCol
{
public:
virtual void a() { cout << "BaseCol::a" << endl; }
virtual void b() { cout << "BaseCol::b" << endl; }
};

class ChildCol1 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol1::a" << endl; }
virtual void b() { cout << "ChildCol1::b" << endl; }
};

class ChildCol2 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol2::a" << endl; }
virtual void b() { cout << "ChildCol2::b" << endl; }
};

class Row : public ChildCol1, public ChildCol2
{
public:
// i want to overide the b function of ChildCol1 only !!
void b() { cout << "Row1::b" << endl; }
};

Now,
In Row class, How do i tell the compiler that i want to overide for
example only the b() function of ChildCol1 base class (and leave the
implementation for this function from ChildCol2) ?


class Row : public ChildCol1, public ChildCol2
{
using ChildCol2::b();
};

Jul 22 '05 #2
Gianni Mariani wrote:
Ronnie wrote:
Please look at the code below,

class BaseCol
{
public:
virtual void a() { cout << "BaseCol::a" << endl; }
virtual void b() { cout << "BaseCol::b" << endl; }
};

class ChildCol1 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol1::a" << endl; }
virtual void b() { cout << "ChildCol1::b" << endl; }
};

class ChildCol2 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol2::a" << endl; }
virtual void b() { cout << "ChildCol2::b" << endl; }
};

class Row : public ChildCol1, public ChildCol2
{
public:
// i want to overide the b function of ChildCol1 only !!
void b() { cout << "Row1::b" << endl; } };

Now,
In Row class, How do i tell the compiler that i want to overide for
example only the b() function of ChildCol1 base class (and leave the
implementation for this function from ChildCol2) ?


class Row : public ChildCol1, public ChildCol2
{
using ChildCol2::b();
};

WRONG ...

It's "using ChildCol2::b" (note the missing '()' ) and this does not do
what I think you want it to do.

You can't really do what you want it to do. If you implement a function
virtual b() in Row - it overrides all virtual "b()" functions.

The way I've done this is:
class BaseCol
{
public:
virtual void a() { cout << "BaseCol::a" << endl; }
virtual void b() { cout << "BaseCol::b" << endl; }
};

class ChildCol1 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol1::a" << endl; }
virtual void b() { b1(); }
virtual void b1() { cout << "ChildCol1::b1" << endl; }
};

class ChildCol2 : public BaseCol
{
public:
virtual void a() { cout << "ChildCol2::a" << endl; }
virtual void b() { b2(); }
virtual void b2() { cout << "ChildCol1::b2" << endl; }

};

class Row : public ChildCol1, public ChildCol2
{
public:
// i want to overide the b function of ChildCol1 only !!
void b1() { cout << "Row1::b" << endl; }
};

In other words - you need to expose a different b.

Jul 22 '05 #3
The function b() will override *both* b functions. Within the code
for the overriden b you could invoke ChildCol::b and ChildCol2::b.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
Jul 22 '05 #4
ev********@hotmail.com (EventHelix.com) wrote in message news:<56**************************@posting.google. com>...
The function b() will override *both* b functions. Within the code
for the overriden b you could invoke ChildCol::b and ChildCol2::b.

Sandeep


So if i write "using" how does this help me overide b function of any
of the multiple base class...please answer with code of overiden
function (one for each base class)

TNX,
Ronnie
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
2
by: junw2000 | last post by:
In the following code: #include <iostream> using namespace std; class V { public: int i; virtual void f() { cout << "V::f()" << endl;}
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
3
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.