I checked that the protected
access specifier can be used inside class
, struct
as well as union
. I know that the protected
access specifier means that members will be private, but visible to derived class. I am not able to think of a reasonable use case where the protected
keyword inside a union would be useful, because a union can't be part of an inheritance hierarchy. Since in a union private
and protected
would make no difference, why allow protected
inside unions too?
The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members.
A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.
The Private Protected keyword combination in the declaration statement specifies that the element can be accessed only from within the same class, as well as from derived classes found in the same assembly as the containing class. The Private Protected access modifier is supported starting with Visual Basic 15.5.
public (C++) When preceding a list of class members, the public keyword specifies that those members are accessible from any function. This applies to all members declared up to the next access specifier or the end of the class.
I'd say the real question is the opposite, why disallow it? Yes, it's synonymous to private
in this case, but would it really be worth the (albeit minor) complication of the language and compiler implementation? It doesn't hurt in any way, either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With