'Can someone explain this C++ syntax?

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.

  1. Is this syntax supported in C?
  2. What would be a reason to use structs over classes?

Thanks a lot in advance.

PS: how do I format the code?

c++


Solution 1:[1]

This is most assuredly just C++. struct and class are identical in C++, except for defaulting to public instead of private for inheritance and class contents.

Solution 2:[2]

In C++, struct and class are essentially the same thing, except that for a struct members are public by default. So just read it as you would a class.

Solution 3:[3]

abc() is a constructor of class abc, member is a internal variable, constructor abc defaults set member as 0.

Solution 4:[4]

  1. The syntax is supported. The constructor initializes member to 0 and does nothing else.
  2. struct has a default access of public.

Solution 5:[5]

This is C++ code, however using typedef struct (that comes from C) in C++ code is awful. There is difference between C and C++ and in C++ you don't need to typedef structs. struct MyStruct is sufficient declaration if you want to refer your struct via MyStruct myStruct;. Mixing C with C++ is bad.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Puppy
Solution 2 Codie CodeMonkey
Solution 3 wuxb
Solution 4 Ted Hopp
Solution 5 shjeff