问题描述
我目前的实现使用了大量具有此语法的复制构造函数
My current implementation uses lots of copy constructors with this syntax
MyClass :: Myclass(Myclass * my_class)
是真的(functionnaly)不同于
Is it really (functionnaly) different from
MyClass :: MyClass const MyClass& my_class)
为什么?
解决方案不是真正的复制构造函数。然而,进行更改意味着相当多的重构。
I was adviced that first solution was not a true copy constructor. However, making the change implies quite a lot of refactoring.
感谢!!!
推荐答案
第一个不是一个复制构造函数,而是一个转换构造函数。它从 MyClass *
转换为 MyClass
。
It's different in the sense that the first isn't a copy constructor, but a conversion constructor. It converts from a MyClass*
to a MyClass
.
根据定义,复制构造函数具有以下签名之一:
By definition, a copy-constructor has one of the following signatures:
MyClass(MyClass& my_class)
MyClass(const MyClass& my_class)
//....
//combination of cv-qualifiers and other arguments that have default values
12.8。复制类对象
12.8. Copying class objects
假设您有:
struct A
{
A();
A(A* other);
A(const A& other);
};
A a; //uses default constructor
A b(a); //uses copy constructor
A c(&a); //uses conversion constructor
它们完全不同的用途。
这篇关于c ++ copy constructor signature:是否重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!