private/protected

S

seesaw

Compared to "public",

how does "private/protected" in inheritance change relationship between
classes

what is the purpose to define constructor as "private/protected"?

is there any usage to define destructor as "private/protected"?

Thanks!
 
J

John Carson

seesaw said:
Compared to "public",

how does "private/protected" in inheritance change relationship
between classes

what is the purpose to define constructor as "private/protected"?

is there any usage to define destructor as "private/protected"?

Thanks!

If you have a laundry list of questions like this, then I suggest you need
to

1. Get a good C++ textbook.
2. Consult the FAQ

http://www.parashift.com/c++-faq-lite/
 
C

Chris Theis

seesaw said:
Compared to "public",

Generally I´d suggest to get a good C++ book (like Accelerated C++ by A.
Koenig & B. Moo) where all these questions are covered in depth. However,
I´ll try to give a short answer which should give you an idea.
how does "private/protected" in inheritance change relationship between
classes

public inheritance represents "is-a" relationship, whereas this is not the
case with private inheritance. This means that a class B which is privately
derived from A is NOT an object of type A and hence cannot be converted to
one (at least not by the compiler). Furthermore, all members inherited from
A are instantly private members of B, no matter of their access type in
class A!

As a consequence of all this the relation of private inheritance is rather a
"implemented in terms of" relation, which of course is something that can
also be achieved by layering. For a detailed discussion of when to use
layering and when to use private inheritance I´d refer you to Effective C++
by Scott Meyers.
what is the purpose to define constructor as "private/protected"?

This way you prevent the user from directly constructing an object.
Consequently you can specify and regulate the way such an object is created
or you can indicate that the user should only use this class as a base
class.
is there any usage to define destructor as "private/protected"?
Yes, there is and I guess you also want to know which :) Declaring a dtor
protected will prevent the deletion of an object via a pointer for example.
Another reason would be to indicate that a certain class can only be used as
a base class.

HTH
Chris
 
D

DaKoadMunky

public inheritance represents "is-a" relationship, whereas this is not the
case with private inheritance. This means that a class B which is privately
derived from A is NOT an object of type A and hence cannot be converted to
one (at least not by the compiler).

The "is-a" relationship does not exist for public users of the derived type,
but it does exist within the derived type and is available to friends of the
derived type who are then freely able to convert between the derived type and
its private base.

A popular C++ author describes this as "controlled polymorphism."

struct Base
{
};

class Derived : private Base
{
friend void FriendOfDerived();
};

void FriendOfDerived()
{
Derived d;
Base *bptr = &d; //Okay! Conversion available here!
}

int main()
{
Derived d;
Base *bptr = &d; //Error! Conversion not available here!

return 0;
}

Is this "controlled polymorphism" actually used in real-word designs?

I would be curious to know.
 

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

Forum statistics

Threads
474,168
Messages
2,570,914
Members
47,455
Latest member
Delilah Code

Latest Threads

Top