Creating an easy to maintain copy constructor

后端 未结 9 2655
夕颜
夕颜 2021-02-19 13:55

Consider the following class:

class A {

char *p;
int a, b, c, d;

public:
   A(const &A);
};

Note that I have to define a copy constructor

9条回答
  •  北恋
    北恋 (楼主)
    2021-02-19 14:30

    Always use RAII objects to manage unmanages resources such as raw pointers, and use exactly one RAII object for each resource. Avoid raw pointers in general. In this case, using std::string is the best solution.

    If that's not possible for some reason, factor the easy to copy parts out into a base class or a member object.

提交回复
热议问题