Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the 'protected' keyword inside a union? [duplicate]

Tags:

c++

c++11

unions

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?

like image 219
anonymous Avatar asked Apr 15 '15 07:04

anonymous


People also ask

What is protected access specifier in C?

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.

What is union keyword C++?

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.

What is private and protected in VB net?

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.

Why do we use public in C++?

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.


1 Answers

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.

like image 86
Angew is no longer proud of SO Avatar answered Sep 18 '22 08:09

Angew is no longer proud of SO