Can C++ struct have member functions?

前端 未结 6 1862
孤街浪徒
孤街浪徒 2021-01-30 13:10

I was pretty confused about the difference between struct and class as I seemed to see them used for pretty much the same things. I googled the differences and the only answer I

6条回答
  •  轮回少年
    2021-01-30 13:44

    I googled the differences and the only answer I saw was that structs have public members by default and classes have private members by default.

    Yes, this is correct. In addition, bases of a struct are inherited publicly by default, whereas bases of a class are inherited privately by default.

    Declaring a function as a member of a struct has precisely the same semantics as declaring a function as a member of a class, except for the difference you've noted. In each case they are called member functions.

提交回复
热议问题