创建用户定义的复制构造函数时无法创建对象
Cannot create object when creating user-defined copy constructor
此代码在尝试添加复制构造函数之前有效。
#include <iostream>
#include <string.h>
using namespace std;
class Laptop
{
public:
string brand;
string model;
int ram;
int storage;
Laptop(string brand, string model, int ram, int storage)
{
this->brand = brand;
this->model = model;
this->ram = ram;
this->storage = storage;
}
Laptop(Laptop &x)
{
this->brand = x.brand;
this->model = x.model;
this->ram = x.ram;
this->storage = x.storage;
}
void displayData()
{
cout << brand << " " << model << ": RAM = " << ram << "GB, Storage = " << storage << "GB" << endl;
}
};
int main()
{
Laptop myLaptop = Laptop("Asus", "Zenbook", 16, 2048);
Laptop copyOriginal = Laptop(myLaptop);
copyOriginal.displayData();
return 0;
}
上面的代码不起作用,只有当我使用以下语法创建 myLaptop
和 oldLaptop
时它才起作用:
Laptop myLaptop("Asus", "Zenbook", 16, 2048);
Laptop copyOriginal(myLaptop);
您不能将 non-constant 引用绑定到临时 object,例如在此声明中,右侧是临时 object
Laptop myLaptop = Laptop("Asus", "Zenbook", 16, 2048);
像这样声明副本
Laptop( const Laptop &x)
{
this->brand = x.brand;
this->model = x.model;
this->ram = x.ram;
this->storage = x.storage;
}
或者添加移动构造函数。
请注意您需要包含 header <string>
而不是 header <string.h>
.
#include <string>
您的 class 可以如下所示
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Laptop
{
public:
string brand;
string model;
int ram;
int storage;
Laptop( const string &brand, const string &model, int ram, int storage)
: brand( brand ), model( model ), ram( ram ), storage( storage )
{
}
Laptop( const Laptop &x)
: brand( x.brand ), model( x.model ), ram( x.ram ), storage( x.storage )
{
}
Laptop( Laptop &&x)
: brand( std::move( x.brand ) ), model( std::move( x.model ) ), ram( x.ram ), storage( x.storage )
{
}
void displayData() const
{
cout << brand << " " << model << ": RAM = " << ram << "GB, Storage = " << storage << "GB" << endl;
}
};
//...
此代码在尝试添加复制构造函数之前有效。
#include <iostream>
#include <string.h>
using namespace std;
class Laptop
{
public:
string brand;
string model;
int ram;
int storage;
Laptop(string brand, string model, int ram, int storage)
{
this->brand = brand;
this->model = model;
this->ram = ram;
this->storage = storage;
}
Laptop(Laptop &x)
{
this->brand = x.brand;
this->model = x.model;
this->ram = x.ram;
this->storage = x.storage;
}
void displayData()
{
cout << brand << " " << model << ": RAM = " << ram << "GB, Storage = " << storage << "GB" << endl;
}
};
int main()
{
Laptop myLaptop = Laptop("Asus", "Zenbook", 16, 2048);
Laptop copyOriginal = Laptop(myLaptop);
copyOriginal.displayData();
return 0;
}
上面的代码不起作用,只有当我使用以下语法创建 myLaptop
和 oldLaptop
时它才起作用:
Laptop myLaptop("Asus", "Zenbook", 16, 2048);
Laptop copyOriginal(myLaptop);
您不能将 non-constant 引用绑定到临时 object,例如在此声明中,右侧是临时 object
Laptop myLaptop = Laptop("Asus", "Zenbook", 16, 2048);
像这样声明副本
Laptop( const Laptop &x)
{
this->brand = x.brand;
this->model = x.model;
this->ram = x.ram;
this->storage = x.storage;
}
或者添加移动构造函数。
请注意您需要包含 header <string>
而不是 header <string.h>
.
#include <string>
您的 class 可以如下所示
#include <iostream>
#include <string>
#include <utility>
using namespace std;
class Laptop
{
public:
string brand;
string model;
int ram;
int storage;
Laptop( const string &brand, const string &model, int ram, int storage)
: brand( brand ), model( model ), ram( ram ), storage( storage )
{
}
Laptop( const Laptop &x)
: brand( x.brand ), model( x.model ), ram( x.ram ), storage( x.storage )
{
}
Laptop( Laptop &&x)
: brand( std::move( x.brand ) ), model( std::move( x.model ) ), ram( x.ram ), storage( x.storage )
{
}
void displayData() const
{
cout << brand << " " << model << ": RAM = " << ram << "GB, Storage = " << storage << "GB" << endl;
}
};
//...