multi inheritance, same virtual function

A

Armel

Hello,

I am in this situation:

class a {
virtual int length( ) const = 0;
};

class b {
virtual int length( ) const = 0;
};

class c: public a, public b
{
virtual int length() const { return something; }
};

is it legal to write that, and will c::length be well considered as the
implementation of a::length and b::length when I make a 'c' object ?

Best regards
Armel
--
 
J

John H.

Hello,

I am in this situation:

class a {
    virtual int length( ) const = 0;

};

class b {
    virtual int length( ) const = 0;

};

class c: public a, public b
{
    virtual int length() const { return something; }

};

is it legal to write that, and will c::length be well considered as the
implementation of a::length and b::length when I make a 'c' object ?

Best regards
Armel
--

I think the answers are yes and yes. An example:
class a {
public:
virtual int length( ) const = 0;
};

class b {
public:
virtual int length( ) const = 0;
};

class c: public a, public b
{
virtual int length() const { return 2; }
};

int main()
{
c my_c;
a & my_a = my_c;
b & my_b = my_c;
int my_a_length = my_a.length(); // 2
int my_b_length = my_b.length(); // 2
return 0;
}
 

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,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top