[comp.lang.c++] derivation from structs

daniel@saturn.ucsc.edu (Daniel Edelson) (05/13/89)

Does derivation from structs rather than classes default
to public rather than private, as it is for classes?

How about if the struct contains a private part?

struct A {
	int x;
};

struct B : A {
};

struct C {
private:
	int x;
};

struct D : C {
};

main()
{
	A * p = new B;	// Is this legal?
	C * q = new D;	// Is this legal?
}

Obviously if this were explicitly public derivation then the 
initializers would be legal, but does the type of derivation default
to public for structs?

daniel edelson
daniel@saturn.ucsc.edu

ark@alice.UUCP (Andrew Koenig) (05/14/89)

In article <7103@saturn.ucsc.edu>, daniel@saturn.ucsc.edu (Daniel Edelson) writes:
> Does derivation from structs rather than classes default
> to public rather than private, as it is for classes?

> How about if the struct contains a private part?

Saying

	struct X { /* stuff */ };

is precisely equivalent to saying

	class X { public: /* stuff */ };

and saying

	struct Y: X { /* stuff */ };

is precisely equivalent to saying

	class Y: public X { public: /* stuff */ };

Whether you said struct or class when defining X is invisible to
any class derived from X.  The only thing that matters is the
actual visibilities that result.
-- 
				--Andrew Koenig
				  ark@europa.att.com

daniel@saturn.ucsc.edu (Daniel Edelson) (05/14/89)

In article <9353@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes:
>In article <7103@saturn.ucsc.edu>, daniel@saturn.ucsc.edu (Daniel Edelson) writes:
>> Does derivation from structs rather than classes default
>> to public rather than private?
>
>Saying
>
>	struct X { /* stuff */ };
>
>is precisely equivalent to saying
>
>	class X { public: /* stuff */ };
>
>and saying
>
>	struct Y: X { /* stuff */ };
>
>is precisely equivalent to saying
>
>	class Y: public X { public: /* stuff */ };
>
>Whether you said struct or class when defining X is invisible to
>any class derived from X.  
>	--Andrew Koenig, ark@europa.att.com

So this means that given

	class X { /* stuff */ };

saying

	struct Y : X { /* more stuff */ };

is precisely equivalent to saying

	struct Y : public X { /* more stuff */ };

and given

	struct X { /* stuff */ };

saying

	class Y : X { /* stuff */ }

is precisely equivalent to saying

	class Y : private X { /* stuff */ }

Correct?

daniel edelson
daniel@saturn.ucsc.edu

ark@alice.UUCP (Andrew Koenig) (05/14/89)

In article <7138@saturn.ucsc.edu>, daniel@saturn.ucsc.edu (Daniel Edelson) writes:

-> So this means that given
-> 
-> 	class X { /* stuff */ };
-> 
-> saying
-> 
-> 	struct Y : X { /* more stuff */ };
-> 
-> is precisely equivalent to saying
-> 
-> 	struct Y : public X { /* more stuff */ };
-> 
-> and given
-> 
-> 	struct X { /* stuff */ };
-> 
-> saying
-> 
-> 	class Y : X { /* stuff */ }
-> 
-> is precisely equivalent to saying
-> 
-> 	class Y : private X { /* stuff */ }
-> 
-> Correct?

Correct, with one implementation note: if you say

	class Y : X { /* stuff */ };

cfront 2.0 will give you a warning.  Too many people are under
the misapprehension that it means the same as

	class Y : public X { /* stuff */ };

so we want to encourage people to say `public' or `private'
explicitly.
-- 
				--Andrew Koenig
				  ark@europa.att.com