C++: When should I use structs instead of classes and where are the speed differences?

前端 未结 5 703
别跟我提以往
别跟我提以往 2021-02-02 10:42
  • When should I use a struct instead of a class? I\'m currently using classes for everything from OpenGL texture wrappers to bitmap fonts.

  • Is

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 11:13

    as others have explained, they're the same thing except for default access levels.

    the only reason why classes can be perceived to be slower is because a good style (but not the only one) is the one mentioned by ArmenTsirunyan: struct for POD types, class for full-fledged object classes. The latter ones usually include inheritance and virtual methods, hence vtables, which are slightly slower to call than straight functions.

提交回复
热议问题