Struct Constructor in C++?

前端 未结 16 1779
轻奢々
轻奢々 2020-11-27 08:53

Can a struct have a constructor in C++?

I have been trying to solve this problem but I am not getting the syntax.

16条回答
  •  [愿得一人]
    2020-11-27 09:43

    In C++ both struct & class are equal except struct'sdefault member access specifier is public & class has private.

    The reason for having struct in C++ is C++ is a superset of C and must have backward compatible with legacy C types.

    For example if the language user tries to include some C header file legacy-c.h in his C++ code & it contains struct Test {int x,y};. Members of struct Test should be accessible as like C.

提交回复
热议问题