Can structs have the same name as a class?
Am I allowed to pass around structs into functions?
Structs are a light-开发者_Python百科weight class? Basically without the functions. It's a container that holds vars. I'm just wondering if I made a function, can I pass in a struct like normally passing in any other object?
In C++, struct
and class
are effectively identical. The only difference is that members of a struct
are public
by default, and members of a class
are private
by default.
You can use them exactly the same way as you use classes. They do need unique names (within the same namespace), so you can't have the struct use the same name as a class.
Yes you can pass structs around like any other variable. As far as I can tell I believe you cannot name them the same as a class but you shouldn't even if you could.
Classes and structs are essentially synonyms in C++; they can have functions, constructors, destructors, operator overloads, inheritance, friends, etc. The only difference is the default access (private
vs. public
, respectively), and the default inheritance type.
So no, they cannot have the same name!
And yes, you can pass a struct/class object by value, pointer or reference into a function.
Basically struct and class are the same. Difference is that struct defaults member to public access
Yes structs can be passed around. The structure should not contain the same name as the class. And here is a thread on passing a struct: Passing a struct with multiple entries in c++
No. Names are unique identifiers. You can't name an int with the same name as a char, can you? (answer is No)
Yes.
Structs can have functions and you can pass them to functions, just like any other variable.
精彩评论