I’m going through someone else’s code and came across the following syntax:
typedef struct abc {
abc() : member(0){}
unsigned int member
}
It seems like a class with member variable and a constructor, except it is declared struct. I have two questions here.
- Is this syntax supported in C?
- What would be a reason to use structs over classes?
Thanks a lot in advance.
PS: how do I format the code?
This is not valid C.
In C++,
structandclassare essentially synonyms. The only difference is that members and inheritance arepublicby default in astruct, andprivateby default in aclass.There are no hard guidelines on whether to choose
structorclass. However, you’ll often find people usingstructonly for simple C-like plain old data structures (“PODs”).