Reputation: 16502
Can structs have the same name as a class?
Am I allowed to pass around structs into functions?
Structs are a light-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?
Upvotes: 1
Views: 444
Reputation: 564861
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.
Upvotes: 5
Reputation: 93468
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.
Upvotes: 0
Reputation: 54094
Basically struct and class are the same. Difference is that struct defaults member to public access
Upvotes: 1
Reputation: 272772
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.
Upvotes: 1
Reputation: 23266
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.
Upvotes: 1
Reputation: 33183
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++
Upvotes: 0