我有一个类,需要一个非默认的复制构造函数和赋值运算符(它包含指针列表).有没有通用的方法来减少复制构造函数和赋值运算符之间的代码重复?
public class SuperCar: Car
{
public bool SuperWheels { get {return true; } }
}
public class Car
{
public bool HasSteeringWheel { get {return true;} }
}
Run Code Online (Sandbox Code Playgroud)
如何设置派生Supercar的基类?
例如,我想简单地设置SuperCars基类,如下所示:
public void SetCar( Car car )
{
SuperCar scar = new SuperCar();
car.Base = car;
}
Run Code Online (Sandbox Code Playgroud)
基本上,如果我有Car对象,我不想手动遍历汽车的每个属性以设置SuperCar对象,我认为这是你可以做到的唯一方法,但如果你能以另一种方式做到这一点会好得多.
我最近发现,当我在一个类中有指针时,我需要指定一个Copy构造函数.
为了解这一点,我做了以下简单的代码.它编译,但在执行复制构造函数时给出了运行时错误.
我试图只复制复制对象的指针中的值,但避免分配相同的地址.
那么,这里有什么问题?
class TRY{
public:
TRY();
~TRY();
TRY(TRY const &);
int *pointer;
void setPointer(int);
};
void TRY::setPointer(int a){
*pointer = a;
return;
}
TRY::TRY(){}
TRY::~TRY(){}
TRY::TRY(TRY const & copyTRY){
int a = *copyTRY.pointer;
*pointer = a;
}
int main(){
TRY a;
a.setPointer(5);
TRY b = a;
b.setPointer(8);
cout << "Address of object a = " << &a << endl;
cout << "Address of object b = " << &b << endl;
cout << "Address of a.pointer = " …Run Code Online (Sandbox Code Playgroud) 可能重复:
我们何时必须使用复制构造函数?
为什么C++拷贝构造函数如此重要?我刚刚了解了它们,我不太清楚它们是什么.如果你使用指针,你似乎应该总是为你的类编写一个复制构造函数,但为什么呢?
谢谢,Boda Cydo.
我已经扩展了std :: string来满足我不得不将自定义函数构建编写到名为CustomString的字符串类中的需求
我已经定义了构造函数:
class CustomString : public std::string {
public:
explicit CustomString(void);
explicit CustomString(const std::string& str);
explicit CustomString(const CustomString& customString);
//assignment operator
CustomString& operator=(const CustomString& customString);
... };
Run Code Online (Sandbox Code Playgroud)
在第三个构造函数(复制构造函数)和赋值运算符中,其定义为:
CustomString::CustomString(const CustomString& customString):
std::string(static_cast<std::string>(customString))
{}
CustomString& CustomString::operator=(const CustomString& customString){
this->assign(static_cast<std::string>(customString));
return *this;
}
Run Code Online (Sandbox Code Playgroud)
首先,因为这是"明确的"; 意味着需要显式转换以分配给另一个CustomString对象; 它抱怨任务.
CustomString s = CustomString("test");
Run Code Online (Sandbox Code Playgroud)
我不确定在哪里确实需要铸造.
如果复制构造函数不是显式的,那么代码可以正常工作,但我想知道并实现显式定义而不是"猜测正确的强制转换".
就像在标题中一样,如何从派生类复制构造函数中调用基类复制构造函数?
第一个例子:
#include <iostream>
#include <memory>
using namespace std;
struct A {
unique_ptr<int> ref;
A(const A&) = delete;
A(A&&) = default;
A(const int i) : ref(new int(i)) { }
~A() = default;
};
int main()
{
A a[2] = { 0, 1 };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它完美地运作.所以这里使用了MOVE构造函数.
让我们删除移动构造函数并添加一个副本:
#include <iostream>
#include <memory>
using namespace std;
struct A {
unique_ptr<int> ref;
A(const A&a)
: ref( a.ref.get() ? new int(*a.ref) : nullptr )
{ }
A(A&&) = delete;
A(const int i) : ref(new …Run Code Online (Sandbox Code Playgroud) 考虑以下程序:
#include <iostream>
struct Test
{
int a;
Test() : a(3)
{ }
Test(const Test& t...)
{
std::cout<<"Copy constructor called\n";
a=t.a;
}
int get_a()
{
return a;
}
~Test()
{
std::cout<<"Destructor is called\n";
}
};
int main()
{
Test t;
Test* t1=new Test(t);
std::cout<<t.get_a()<<'\n';
std::cout<<t1->get_a()<<'\n';
delete t1;
}
Run Code Online (Sandbox Code Playgroud)
仔细观察复制构造函数参数中的三个点我尝试这个程序时真的很惊讶.有什么用?这是什么意思?
语言规范对此有何看法?
我知道三个点用于表示变量函数(如等)中的变长参数printf()以及scanf()C99引入的可变参数宏.在C++中,如果我没有错,它们将用于可变参数模板.
这段代码是否形成良好?这个可变参数构造函数是否可以使用任意数量的参数?
它在g ++ 4.8.1和MSVS 2010上编译并运行良好.
为了正确处理对象复制,经验法则是三规则.使用C++ 11,移动语义是一个东西,所以它是五条规则.然而,在这里和互联网上的讨论中,我也看到了对四条规则(一半)的引用,它是五条规则和复制交换习语的组合.
究竟是什么(四分之一)呢?需要实现哪些功能,每个功能的主体应该是什么样的?一半的功能是什么?与五法则相比,这种方法有任何缺点或警告吗?
这是一个类似于我当前代码的参考实现.如果这不正确,那么正确的实现会是什么样的?
//I understand that in this example, I could just use `std::unique_ptr`.
//Just assume it's a more complex resource.
#include <utility>
class Foo {
public:
//We must have a default constructor so we can swap during copy construction.
//It need not be useful, but it should be swappable and deconstructable.
//It can be private, if it's not truly a valid state for the object.
Foo() : resource(nullptr) {}
//Normal …Run Code Online (Sandbox Code Playgroud) c++ copy-constructor assignment-operator copy-and-swap c++11
我在Visual Studio 2017上.最近,因为我不喜欢C++的不符合标准,所以我继续并禁用了选项中的非标准语言扩展.到现在为止还挺好.现在我有一个问题.
#include <iostream>
#include <vector>
struct Vertex
{
Vertex(float pos) { }
Vertex(Vertex& other) { }
};
std::vector<Vertex> arrayOfVertices;
int main()
{
arrayOfVertices.emplace_back(7.f);
}
Run Code Online (Sandbox Code Playgroud)
这不会在Visual Studio中编译,它给出的唯一错误是:
"编译器中发生内部错误"
如果我启用语言扩展,它编译得很好.如果我禁用语言扩展并使复制构造函数采用const Vertex&它编译好.
所以我在一些在线编译器上尝试了GCC,如果复制构造函数没有采用const引用参数,它将无法编译,从而产生各种错误.似乎最有意义的是:
错误:从'Vertex'类型的右值开始无效初始化'Vertex&'类型的非const引用
我认为复制构造函数不必是const,在我的情况下我想修改另一个引用中的东西.我知道非const参数不能采用r值引用,但我测试了它,结果发现在vector::emplace_back()拷贝构造函数中根本没有调用:
#include <iostream>
#include <vector>
struct Vertex
{
Vertex(float pos)
{
std::cout << "Calling constructor\n";
}
Vertex(const Vertex& other)
{
std::cout << "Calling copy constructor\n";
}
};
std::vector<Vertex> arrayOfVertices;
int main()
{
arrayOfVertices.emplace_back(7.f); // Normal constructor called if const,
// doesn't compile if …Run Code Online (Sandbox Code Playgroud) copy-constructor ×10
c++ ×9
c++11 ×3
c# ×1
constructor ×1
dry ×1
explicit ×1
inheritance ×1
pointers ×1
reflection ×1
stdstring ×1