In C++ (and especially C), types are usually always represented by their construct.
For instance,
enum Foo { ... };
void doSomethingWithAFoo(enum Foo f);
struct Bar { ... };
void doSomethingWithABar(struct Bar bar);
While this is required in C, it is not in C++. In C, it is achieved by using a typedef
.
typedef struct { ... } Foo; // Can now be referenced with just `Foo`
However, there is a particular part of the spec that states that struct types cannot have instance of themselves inside of them (more specifically it states types cannot refer to themselves before they're fully declared).
Except in pointer form. This is because pointers are known sizes at the beginning of compilation, whereas structures are only known after they are declared.
Since struct
s pre-date C++ (only by a little) and have been present since ANSI C (C89) and before in most major compilers, they are also present in C++ (since ANSI C can be compiled gracefully in compliant C++ compilers).
However, C++ adds the concept of classes, which don't exist in C. As others have mentioned, classes and structs are similar in that they both holds members. In C++, structs can have methods just like classes - obviously this is not the case in C.
The only difference as far as I'm aware is the visibility; struct
s default to public and class
es default to private. C does not have the notion of visibility.