小编Fat*_*imi的帖子

即使没有任何要复制的对象,也会自动调用复制构造函数?

我在网上发现了这段代码:

    #include <iostream>

using namespace std;

class Line {
   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len) {
   cout << "Normal constructor allocating ptr" << endl;

   // allocate memory for the pointer;
   ptr = new int;
   *ptr = len;
}

Line::Line(const Line &obj) {
   cout << "Copy constructor allocating ptr." << endl;
   ptr = …
Run Code Online (Sandbox Code Playgroud)

c++ constructor copy-constructor

0
推荐指数
1
解决办法
178
查看次数

标签 统计

c++ ×1

constructor ×1

copy-constructor ×1