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

后端 未结 25 2114
误落风尘
误落风尘 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:50

    I use struct only when I need to hold some data without any member functions associated to it (to operate on the member data) and to access the data variables directly.

    Eg: Reading/Writing data from files and socket streams etc. Passing function arguments in a structure where the function arguments are too many and function syntax looks too lengthy.

    Technically there is no big difference between class and struture except default accessibility. More over it depends on programming style how you use it.

提交回复
热议问题