Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default visibility of C++ class/struct members

In C++, why is private the default visibility for members of classes, but public for structs?

like image 202
S I Avatar asked Aug 08 '09 01:08

S I


People also ask

What is the default visibility of class members?

The access level for class members and struct members, including nested classes and structs, is private by default.

Are struct members public by default?

A structure is a class defined with the struct keyword. Its members and base classes are public by default. In practice, structs are typically reserved for data without functions.

Are struct members private by default?

(B) Members of a class are private by default and members of struct are public by default. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public and when deriving a class, default access specifier is private.

Are structs always public?

Structs, on the other hand, have to be compatible with C structs, which are always public (there is no notion of public and private in C), and don't use accessors/mutators.

Why are C++ structs private by default?

A whole lot of C code exists, including libraries that were desired to work with C++ as well, that use structs. Classes were introduced in C++, and to conform with the OO philosophy of encapsulation, their members are private by default. Show activity on this post.

What are visibility modes in C++?

Visibility Modes in C++ with Examples. When a base class is derived by a derived class with the help of inheritance, the accessibility of base class by the derived class is controlled by visibility modes. The derived class doesn’t inherit access to private data members. However, it does inherit a full parent object, ...

What is the default access level of a class member?

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified. ... The access level for class members and struct members, including nested classes and structs, is private by default.

Do structs exist in C/C++?

A whole lot of C code exists, including libraries that were desired to work with C++ as well, that use structs. Classes were introduced in C++, and to conform with the OO philosophy of encapsulation, their members are private by default.


2 Answers

C++ was introduced as a superset of C. Structs were carried over from C, where the semantics of their members was that of public. A whole lot of C code exists, including libraries that were desired to work with C++ as well, that use structs. Classes were introduced in C++, and to conform with the OO philosophy of encapsulation, their members are private by default.

like image 112
Oren Trutner Avatar answered Sep 23 '22 20:09

Oren Trutner


Because a class is a usual way of doing object orientation, which means that member variables should be private and have public accessors - this is good for creating low coupling. Structs, on the other hand, have to be compatible with C structs, which are always public (there is no notion of public and private in C), and don't use accessors/mutators.

like image 45
a_m0d Avatar answered Sep 22 '22 20:09

a_m0d