Search code examples
c++classstructaccess-levels

Default security parameter in classes


I have noticed in a large amount of code that people declare their classes public before writing code into it. Is this a preference or is there more meaning to it? It seems kind of superfluous to me being that struct's default parameter is public and does exactly what a class does (as I have read).

code example

class box
{
public:
void setParam( int x, int y) { . . . }
int getWidth(){ . . . }
int getHeight(){ . . . }
private:
int width, height;
}

Could be done without declaring it public in a struct.


Solution

  • Generally its a matter of convention/semantics than it is because of a technical reason.

    The rationale is usually that structs are reserved for POD usage, helper data structures, "data payload"s, functors etc. Whereas classes are for types that encapsulate non-trivial operations that usually have to respect some type of class invariant.