By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

Overiding a virtual function of multiple inheritance base class

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.