如何在没有为赋值运算符重载的情况下运行此代码

时间:2015-01-20 01:43:21

标签: c++ oop c++11 constructor

我想知道这个代码如何专门运行第54行(第2行=第1行),尽管赋值运算符没有重载? 从输出看来,复制构造函数和普通构造函数都没有被调用,令人惊讶的是它按预期输出199 199

#include <iostream>

using namespace std;

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

   private:
      int *ptr;
};

Line::Line(int len)
{
    cout << "Normal constructor allocating ptr" << endl;
    ptr = new int;
    *ptr = len;
}

Line::Line(const Line &obj)
{
    cout << "Copy constructor allocating ptr." << endl;
    ptr = new int;
   *ptr = *obj.ptr;
}

Line::~Line(void)
{
    cout << "Freeing memory!" << endl;
    delete ptr;
}
int Line::getLength()
{
    return *ptr;
}

void display(Line obj)
{
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main()
{
   Line line1(199);

   Line line2(1);
   line2 = line1; // How this is executed ??!
   cout << line1.getLength() << " " << line2.getLength() << endl ;
   /*display(line1);
   display(line2);*/

   cin.get();
   return 0;
}

3 个答案:

答案 0 :(得分:6)

你有什么未定义的行为。您指定line2 = line1但没有用户定义的赋值运算符,因此您使用编译器提供的默认值。默认设置只复制所有字段,在您的情况下包含int*。这会为您提供相同int*的两个副本,泄漏line2之前指向的值,并最终加倍 - delete最初指向的line1。当delete超出line1末尾的范围时,同一指针的第二个main()将调用未定义的行为。

如果你有一个释放资源的析构函数,你可能也需要一个赋值运算符。参见三法则:http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

但最好的解决方案是停止使用原始指针。使用智能指针,这个问题首先不会发生,你可以省略你的析构函数。

答案 1 :(得分:4)

在这样的情况下,编写自己的复制构造函数,赋值运算符和析构函数应该是你最后的选择,而不是你的第一反应。

你的第一反应通常是要使用一些已经为你处理过这些杂务的预定义类。在这种情况下,从原始指针更改为shared_ptr(仅限一种可能性)可以相当快地清除代码。使用它,代码最终会是这样的:

#include <iostream>
#include <memory>

using namespace std;

class Line
{
   public:
      int getLength();
      Line( int len );             // simple constructor
      ~Line();                     // destructor

      // copy constructor removed, because the one supplied by the 
      // compiler will be fine. Likewise the compiler-generated assignment
      // operator.
   private:
      shared_ptr<int> ptr;
};

Line::Line(int len)
{
    cout << "Normal constructor allocating ptr" << endl;

    // Note the use of make_shared instead of a raw `new`
    ptr = make_shared<int>(len);
}

Line::~Line(void)
{
    cout << "Freeing memory!" << endl;
    // don't need to actually do anything--freeing is automatic
}
int Line::getLength()
{
    return *ptr;
}

void display(Line obj)
{
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main()
{
   Line line1(199);

   Line line2(1);
   line2 = line1; // uses compiler-generated assignment operator (which works)
   cout << line1.getLength() << " " << line2.getLength() << endl ;
   display(line1);
   display(line2);

   cin.get();
   return 0;
}

根据具体情况,unique_ptr可能比shared_ptr更合适。在这种情况下,shared_ptr可能更容易合并到现有代码中。

您可能还想阅读R. Martinho Fernandes关于此主题的Rule of Zero博客文章。

答案 2 :(得分:3)

如果没有为类类型(struct,class或union)提供用户定义的复制赋值运算符,则编译器将始终将one声明为该类的内联公共成员。

如果满足以下所有条件,则此隐式声明的复制赋值运算符的格式为T& T::operator=(const T&)

  1. T的每个直接基数B都有一个复制赋值运算符,其参数为B或const B&const volatile B&

  2. 类类型的T或类类型数组的每个非静态数据成员M都有一个复制赋值运算符,其参数为Mconst M&const volatile M&

  3. 否则隐式声明的复制赋值运算符被声明为T& T::operator=(T&)。 (请注意,由于这些规则,隐式声明的复制赋值运算符无法绑定到volatile lvalue参数)

    从CPPReference的this article复制。