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.