Default Visibility of C++ Class/Struct Members

default visibility of C++ class/struct members

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.

Default visibility for C# classes and members (fields, methods, etc.)?

All of the information you are looking for can be found here and here (thanks Reed Copsey):

From the first link:

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.

...

interfaces default to internal access.

...

Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.


From the second link:

Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.

And for nested types:


Members of Default member accessibility
---------- ----------------------------
enum public
class private
interface public
struct private

When should you use a class vs a struct in C++?

The differences between a class and a struct in C++ are:

  • struct members and base classes/structs are public by default.
  • class members and base classes/structs are private by default.

Both classes and structs can have a mixture of public, protected and private members, can use inheritance, and can have member functions.

I would recommend you:

  • use struct for plain-old-data structures without any class-like features;
  • use class when you make use of features such as private or protected members, non-default constructors and operators, etc.

Protection level of a struct field within a class

The other answers given already have your answer, so I won't beat a dead horse here. You need to declare the field public in order to be able to access it from external code.

In C++, structs and classes are equivalent, with the only difference being the default access level of their respective members.

However, that's not the case in C#. Generally, you should only use a struct for small, short-lived objects that are immutable (won't change). A structure has value type semantics, where as a class has reference type semantics. It's very important that you understand the difference between value types and reference types if you're learning to program in C#. Jon Skeet has published an article that attempts to provide that explanation. But you'll definitely want to pick up a good introductory book to C# that treats these issues in more detail.

More often than not, you'll want to use a class in C#, rather than a structure. And when you use that class, note that Microsoft's design guidelines for C# tend to recommend against exposing public fields. Instead, they recommend that you use a public property, backed by a private field. A more thorough exposition of the rationale behind that guideline is given here. For example:

class TimePeriod
{
private double seconds;

public double Seconds
{
get { return seconds; }
set { seconds = value; }
}
}

Or you can use the simpler "automatic properties" syntax, which has the compiler automatically generate that private backing field:

class TimePeriod
{
public double Seconds { get; set; }
}

C/C++ Struct vs Class

In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.

However, in C, a struct is just an aggregate collection of (public) data, and has no other class-like features: no methods, no constructor, no base classes, etc. Although C++ inherited the keyword, it extended the semantics. (This, however, is why things default to public in structs—a struct written like a C struct behaves like one.)

While it's possible to fake some OOP in C—for instance, defining functions which all take a pointer to a struct as their first parameter, or occasionally coercing structs with the same first few fields to be "sub/superclasses"—it's always sort of bolted on, and isn't really part of the language.

C++ Private Structures

Yes structures can have private members, you just need to use the access specifier for the same.

struct Mystruct
{
private:
m_data;

};

Only difference between structure and class are:

  • access specifier defaults to private for class and public for struct
  • inheritance defaults to private for class and public for struct

How can you access them?

Just like you access private members of a class. i.e: they can only be accessed within the structures member functions and not in derived structure etc.

What are the differences between struct and class in C++?

You forget the tricky 2nd difference between classes and structs.

Quoth the standard (§11.2.2 in C++98 through C++11):

In absence of an access-specifier
for a base class, public is assumed
when the derived class is declared
struct and private is assumed when the class is declared class.

And just for completeness' sake, the more widely known difference between class and struct is defined in (11.2):

Member of a class defined with the
keyword class are private by
default. Members of a class defined
with the keywords struct or union
are public by default.

Additional difference: the keyword class can be used to declare template parameters, while the struct keyword cannot be so used.



Related Topics



Leave a reply



Submit