Overiding a virtual function of multiple inheritance base class

R

Ronnie

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
 
G

Gianni Mariani

Ronnie said:
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();
};
 
G

Gianni Mariani

Gianni said:
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.
 
E

EventHelix.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
 
R

Ronnie

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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top