Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overiding a virtual function of multiple inheritance base class

Reply
Thread Tools

Overiding a virtual function of multiple inheritance base class

 
 
Ronnie
Guest
Posts: n/a
 
      11-18-2003
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
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      11-18-2003
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();
};

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      11-18-2003
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.

 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      11-18-2003
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
 
Reply With Quote
 
Ronnie
Guest
Posts: n/a
 
      11-19-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (EventHelix.com) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual inheritace -- when one inheritance of the base is virtual andthe other isn't. pauldepstein@att.net C++ 1 03-14-2009 03:45 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
How to restrict overiding a function in derived class? Pankh C++ 2 09-17-2004 08:44 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM



Advertisments