Connecting Tech Pros Worldwide Forums | Help | Site Map

private/protected

seesaw
Guest
 
Posts: n/a
#1: Jul 22 '05
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!



John Carson
Guest
 
Posts: n/a
#2: Jul 22 '05

re: private/protected


"seesaw" <seesaw@turboweb.com> wrote in message
news:2_%jc.33968$_o3.1102127@bgtnsc05-news.ops.worldnet.att.net[color=blue]
> 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![/color]

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/


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Chris Theis
Guest
 
Posts: n/a
#3: Jul 22 '05

re: private/protected



"seesaw" <seesaw@turboweb.com> wrote in message
news:2_%jc.33968$_o3.1102127@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Compared to "public",[/color]

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.
[color=blue]
>
> how does "private/protected" in inheritance change relationship between
> classes[/color]

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.
[color=blue]
>
> what is the purpose to define constructor as "private/protected"?[/color]

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.
[color=blue]
>
> is there any usage to define destructor as "private/protected"?
>[/color]
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


DaKoadMunky
Guest
 
Posts: n/a
#4: Jul 22 '05

re: private/protected


>public inheritance represents "is-a" relationship, whereas this is not the[color=blue]
>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).[/color]

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.



Closed Thread