1

Can I build a constructor to initialize a struct this way:

mystruct struct1(a,b); 

the same way I initialize a class?

Or do I have to use this way:

mystruct struct1=mystruct(a,b);  

?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
lital maatuk
  • 5,921
  • 20
  • 57
  • 79
  • 3
    The only difference between a class and a struct in C++ is that struct members are public by default, whereas class members are private by default. Other than that, the 2 concepts are interchangeable. – Paul Keister Feb 06 '11 at 08:23
  • 1
    @PaulKeister: Members and bases. – Fred Nurk Feb 06 '11 at 10:57

3 Answers3

4

You can use the same syntax as you use for class. In C++, there is no difference between them except for the default access specifiers which is public for struct and private for class. See here for detailed explaination: Difference between struct and class

Community
  • 1
  • 1
Naveen
  • 74,600
  • 47
  • 176
  • 233
2

In C++ there is no difference between a structure and a class except that the data members by default are public in case of struct and private in case of class.

Furthermore there are two common modes of initialization of objects in C++

1) Direct Initialization
2) Copy Initialization

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
0

Both ways are valid. you can do it either ways.