struct like class?

closed account (zwA4jE8b)
I have seen (from the forums) struct's with class like properties. Are structs and classes similar in that they can contain functions and destructors and the such?

It sounds like they are similar in that they are both objects w/ properties.

What are the differences? Similarities?

Thanks,
Mike
The only difference between a struct and a class in C++ is that in a struct members are public by default, in a class they are private
closed account (zwA4jE8b)
cool. thank you!
Furthermore, it has been suggested to use structs as aggregate types, i.e. types that only store data and expose no interface other than their public members. If you simply want to bundle a bunch of data in an organized typed use structs instead of classes. If you want some semantics connected to your type, for example a method normalize() in a mathematical vector class, use a class.

Example:
1
2
3
4
5
struct Person
{
    std::string name;
    int age;
};
Last edited on
Topic archived. No new replies allowed.