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

后端 未结 25 1711
误落风尘
误落风尘 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: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()); 
    

提交回复
热议问题