When should you use a class vs a struct in C++?

后端 未结 25 1685
误落风尘
误落风尘 2020-11-22 00:18

In what scenarios is it better to use a struct vs a class in C++?

相关标签:
25条回答
  • 2020-11-22 00:41

    Differences between a class and a struct in C++ are that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public, protected and private members, can use inheritance and can have member functions.

    I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.

    0 讨论(0)
  • 2020-11-22 00:41

    They are pretty much the same thing. Thanks to the magic of C++, a struct can hold functions, use inheritance, created using "new" and so on just like a class

    The only functional difference is that a class begins with private access rights, while a struct begins with public. This is the maintain backwards compatibility with C.

    In practice, I've always used structs as data holders and classes as objects.

    0 讨论(0)
  • 2020-11-22 00:41

    I thought that Structs was intended as a Data Structure (like a multi-data type array of information) and classes was inteded for Code Packaging (like collections of subroutines & functions)..

    :(

    0 讨论(0)
  • 2020-11-22 00:42

    Technically both are the same in C++ - for instance it's possible for a struct to have overloaded operators etc.

    However :

    I use structs when I wish to pass information of multiple types simultaneously I use classes when the I'm dealing with a "functional" object.

    Hope it helps.

    #include <string>
    #include <map>
    using namespace std;
    
    struct student
    {
        int age;
        string name;
        map<string, int> grades
    };
    
    class ClassRoom
    {
        typedef map<string, student> student_map;
      public :
        student getStudentByName(string name) const 
        { student_map::const_iterator m_it = students.find(name); return m_it->second; }
      private :
        student_map students;
    };
    

    For instance, I'm returning a struct student in the get...() methods over here - enjoy.

    0 讨论(0)
  • 2020-11-22 00:43

    The only time I use a struct instead of a class is when declaring a functor right before using it in a function call and want to minimize syntax for the sake of clarity. e.g.:

    struct Compare { bool operator() { ... } };
    std::sort(collection.begin(), collection.end(), Compare()); 
    
    0 讨论(0)
  • 2020-11-22 00:44

    For C++, there really isn't much of a difference between structs and classes. The main functional difference is that members of a struct are public by default, while they are private by default in classes. Otherwise, as far as the language is concerned, they are equivalent.

    That said, I tend to use structs in C++ like I do in C#, similar to what Brian has said. Structs are simple data containers, while classes are used for objects that need to act on the data in addition to just holding on to it.

    0 讨论(0)
提交回复
热议问题