Creating a structure using the keyword struct

Suppose I declare a new type of C ++ structure:

struct my_struct { int a; int b; }; 

Can I create a new instance of this type of structure with:

 my_struct foo; 

or

 struct my_struct foo; 

If both work, is there a difference?

+4
source share
2 answers

Yes, you can use any of these methods. The difference is that this form:

 my_struct foo; 

It is not legal in C, so you should use this form:

 struct my_struct foo; 

What is supported in C ++ for backward compatibility with C.

+14
source

Both work. The main reason for the second work is compatibility with C: In C the first does not work. As a result, struct usually typedef ed in C and there are two different name types for struct and typedef s.

+2
source

Source: https://habr.com/ru/post/1438311/


All Articles