What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience?

前端 未结 16 1809
刺人心
刺人心 2021-01-29 19:48

What are some C++ related idioms, misconceptions, and gotchas that you\'ve learnt from experience?

An example:

class A
{
  public: 
  char s[1024];
  cha         


        
16条回答
  •  离开以前
    2021-01-29 20:15

    If you have a class which does not have value semantics, make sure that all of the following constructs are explicitly declared in order to prevent headaches down the road.

    • Default Constructor
    • Copy Constructor
    • Assignment Operator

    In many cases you only need to declare a subset of these constructs. However it can get really tricky in some cases as to which are needed and which are not. It's much safer to declare all 3 private and be done with the matter.

    It's also very helpful to add a comment to the top explaining that this is not a copy safe class.

    This will save you time down the road.

提交回复
热议问题