【问题标题】:Understanding working of copy constructor了解复制构造函数的工作
【发布时间】:2020-08-28 06:32:07
【问题描述】:

为了理解构造函数、复制构造函数、析构函数的工作原理,我编写了以下代码。

#include <iostream>
 using namespace std; 
 class Cat
{
 public:
  Cat();
  Cat(Cat&);
  ~Cat();
  int itsage;
};
 
 Cat::Cat()
{
 cout << "Constructor called\n";
 cout << this << endl;
 itsage=2;
}
 
 Cat::Cat(Cat& theCat)
{
 cout << "Copy constructor called\n";
 cout << this << endl;
 itsage=theCat.itsage;
}

 Cat::~Cat()
{
 cout << "Destructor called\n";
 cout << this << endl;
}
 
 Cat myFunction(Cat Frisky)
{
 cout << "Inside myFunction\n";
 cout << "Frisky's address : " << &Frisky ; 
 cout << "\nFrisky's age: " << Frisky.itsage << "\n";
 Frisky.itsage=100;
 cout << "Reassigned Frisky's age: "<< Frisky.itsage << "\n";
 return Frisky;
}
 
 int main()
{
 Cat Mani;
 cout << "Mani's address : " << &Mani ;
 cout << "\nMani's age: " << Mani.itsage << "\n";
 myFunction(Mani);
 return 0;
}

我得到如下输出:

Constructor called
0x61ff04
Mani's address : 0x61ff04
Mani's age: 2
Copy constructor called
0x61ff0c
Inside myFunction
Frisky's address : 0x61ff0c
Frisky's age: 2
Reassigned Frisky's age: 100
Copy constructor called
0x61ff08
Destructor called
0x61ff08
Destructor called
0x61ff0c
Destructor called
0x61ff04

除了在第二次调用复制构造函数时存储在地址0x61ff08 的内容之外,一切对我来说都很好?意味着我们可以看到存储在地址0x61ff0c0x61ff04 中的内容,它们只不过是FriskyMani。那0x61ff08那个看不见的东西是什么?

我想通过在 main 函数中做一些小改动来使该对象可见。

#include <iostream>
 using namespace std; 
 class Cat
{
 public:
  Cat();
  Cat(Cat&);
  ~Cat();
  int itsage;
};
 
 Cat::Cat()
{
 cout << "Constructor called\n";
 cout << this << endl;
 itsage=2;
}
 
 Cat::Cat(Cat& theCat)
{
 cout << "Copy constructor called\n";
 cout << this << endl;
 itsage=theCat.itsage;
}

 Cat::~Cat()
{
 cout << "Destructor called\n";
 cout << this << endl;
}
 
 Cat myFunction(Cat Frisky)
{
 cout << "Inside myFunction\n";
 cout << "Frisky's address : " << &Frisky ; 
 cout << "\nFrisky's age: " << Frisky.itsage << "\n";
 Frisky.itsage=100;
 cout << "Reassigned Frisky's age: "<< Frisky.itsage << "\n";
 return Frisky;
}
 
 int main()
{
 Cat Mani;
 cout << "Mani's address : " << &Mani ;
 cout << "\nMani's age: " << Mani.itsage << "\n";
 Cat Sweety = myFunction(Mani); 
 cout << "Sweety's age : " << Sweety.itsage ;
 return 0;
}

但收到如下错误:

ppp.cpp: In function 'int main()':
ppp.cpp:47:25: error: invalid initialization of non-const reference of type 'Cat&' from an rvalue of type 'Cat'
  Cat Sweety = myFunction(Mani);
               ~~~~~~~~~~^~~~~~
ppp.cpp:19:2: note:   initializing argument 1 of 'Cat::Cat(Cat&)'
  Cat::Cat(Cat& theCat)
  ^~~

我真的不明白出了什么问题。

【问题讨论】:

    标签: c++ copy-constructor


    【解决方案1】:

    您的第一个代码从myFunction 返回Frisky 的副本,这就是额外副本的来源。

    您的第二个代码不起作用,因为您的复制构造函数错误地采用了非常量引用,并且您无法将从 myFunction 返回的临时值传递给非常量引用。

    与您的问题没有直接关系,但您应该遵守rule of three 并实现赋值运算符。

    【讨论】:

    • 有点奇怪.. 当我们通过Mani 时,复制构造函数是可以的,但是当我们返回Frisky 时出现问题.. 为什么会这样?
    • Mani 不是临时的,因此可以绑定到非常量引用
    • 谢谢.. 最后一件事.. 可能听起来很难看,但我有一段时间一直在怀疑.. Frisky 的副本存储在0x61ff08,后来它也被销毁了。 . 那么Mani 的副本(它是在我们调用myFunction 时创建的).. 它不会存储在某个地方或被销毁吗?
    • Mani的副本是Frisky,存放在myFunction的栈上,函数结束时销毁
    【解决方案2】:
    Cat(Cat &);
    

    在此处使用const

    Cat(const Cat &);
    

    不相关,但考虑使用这样的初始化。

    Cat Sweety{myFunction(Mani)};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-09
      • 2018-09-23
      • 1970-01-01
      • 2017-10-30
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多