Struct Constructor in C++?

Jay picture Jay · Jul 14, 2009 · Viewed 606.6k times · Source

Can a struct have a constructor in C++?

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

Answer

sth picture sth · Jul 14, 2009

In C++ the only difference between a class and a struct is that members and base classes are private by default in classes, whereas they are public by default in structs.

So structs can have constructors, and the syntax is the same as for classes.