when to use private inheritance?

A

al pacino

hi,
is there any advantage of using private inheritance over composition
and vice versa.
under what condition/s should we chose one over the other.

thanks in advance.
 
I

Ian Collins

al said:
hi,
is there any advantage of using private inheritance over composition
and vice versa.
under what condition/s should we chose one over the other.
I tend to prefer private inheritance

a) when 'is a' makes more sense than 'has a'.
b) it could go either way and I want to save on typing.
 
D

Daniel T.

"al pacino said:
hi,
is there any advantage of using private inheritance over composition
and vice versa.
under what condition/s should we chose one over the other.

class Base {
public:
// lots of functionality
virtual void fun();
};

Now, assume we want to write a class that can use Base's functionality
and must override fun, but shouldn't be useable as a Base*. We have two
choices.

class Derived : public Base {
public:
void fun();
};

class User {
Derived d;
public:
//stuff
};

or simply:

class User : private Base {
void fun();
public:
// stuff
};
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top