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

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

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

25条回答
  •  鱼传尺愫
    2020-11-22 01:00

    When would you choose to use struct and when to use class in C++?

    I use struct when I define functors and POD. Otherwise I use class.

    // '()' is public by default!
    struct mycompare : public std::binary_function
    {
        bool operator()(int first, int second)
        { return first < second; }
    };
    
    class mycompare : public std::binary_function
    {
    public:
        bool operator()(int first, int second)
        { return first < second; }
    };
    

提交回复
热议问题