Creating an easy to maintain copy constructor

后端 未结 9 2664
夕颜
夕颜 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:18

    As a rule of thumb: If you have to manually manage resources, wrap each into its own object.

    Put that char* into its own object with a proper copy constructor and let the compiler do the copy constructor for A. Note that this also deals with assignment and destruction, which you haven't mentioned in your question, but need to be dealt with nevertheless.
    The standard library has several types to pick from for that, among them std::string and std::vector.

提交回复
热议问题