gpt4 book ai didi

c++ - 了解复制构造函数的工作

转载 作者:行者123 更新时间:2023-12-02 10:27:38 25 4
gpt4 key购买 nike

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

#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)
^~~

我真的没弄错。

最佳答案

您的第一个代码从Frisky返回myFunction的副本,这是附加副本的来源。
您的第二个代码无效,因为您的副本构造函数错误地采用了非常量引用,并且您无法将myFunction返回的临时值传递给非常量引用。
与您的问题没有直接关系,但是您应该服从rule of three并实现赋值运算符。

关于c++ - 了解复制构造函数的工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63628639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com