什么时候使用拷贝构造函数?
When to use a copy constructor?
我创建了一个 class,它有一个带参数的构造函数。我认为我在这里需要一个复制构造函数是对的吗?
我有一个class,不接受指点:
class xyz : public Node
{
public:
xyz( uint8_t node);
xyz(const xyz& cxyz);
~xyz();
private:
uint8_t m_node;
uint16_t m_gainxyz;
和基地:
class Node
{
public:
Node();
virtual ~Node();
protected:
std::string m_name;
当我这样做时:
xyz xyz = Initxyz(node);
编译器告诉我创建一个拷贝构造函数。
其中:
xyz PD::Initxyz(Source& inputNode)
{
if (inputNode.getNodeNumber() > 10 )
{
xyz element(inputNode.getNodeNumber());
element.setInputNode(inputNode);
return element;
}
else
{
std::cout << "ERROR IN XYZ CONFIGURATION" << std::endl;
//Throw Exception;
}
}
但是根据我在网上看到的:
If the object has no pointers to dynamically allocated memory, a shallow copy is probably sufficient. Therefore the default copy constructor, default assignment operator, and default destructor are ok and you don't need to write your own.
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html
如果我使用带参数的构造函数,我是否也必须有一个?
我会提供一些规范的参考,以便正式地解释这件事。
如果我正确理解你想要什么,当你需要复制你的对象时 N4296::12.8/1 [class.copy]
:
A class object can be copied or moved in two ways: by initialization
(12.1, 8.5), including for function argument passing (5.2.2) and for
function value return (6.6.3); and by assignment (5.18).
实际上,复制构造函数可能被隐式声明为已删除,因此如果您在需要复制时尝试调用它,将会出现编译时错误。
相关参考:
An implicitly-declared copy/move constructor is an inline public
member of its class. A defaulted copy/move constructor for a class X
is defined as deleted (8.4.3) if X has:
(11.1) — a variant member with a non-trivial corresponding constructor
and X is a union-like class,
(11.2) — a potentially constructed
subobject type M (or array thereof) that cannot be copied/moved
because overload resolution (13.3), as applied to M’s corresponding
constructor, results in an ambiguity or a function that is deleted or
inaccessible from the defaulted constructor,
(11.3) — any potentially
constructed subobject of a type with a destructor that is deleted or
inaccessible from the defaulted constructor, or,
(11.4) — for the copy
constructor, a non-static data member of rvalue reference type.
其中潜在构造子对象的定义如下N4296::12/5
For a class, its non-static data members, its non-virtual direct base
classes, and, if the class is not abstract (10.4), its virtual base
classes are called its potentially constructed subobjects.
I have created a class, which has a constructor that takes a parameter. Am I right in thinking that I need a copy constructor here?
您不需要为您的类提供复制构造函数。编译器生成的就足够了。
这里是a working example based on your code:
#include <iostream>
#include <string>
#include <stdint.h>
class Node
{
public:
Node() {}
virtual ~Node() {}
protected:
std::string m_name;
};
class xyz : public Node
{
public:
xyz( uint8_t node) {}
private:
uint8_t m_node;
uint16_t m_gainxyz;
};
xyz Initxyz(Node)
{
return xyz(42);
}
int main()
{
Node node;
xyz xyz = Initxyz(node);
}
当你想做深拷贝而不是浅拷贝时需要复制构造函数copy.It意味着你正在避免悬空指针问题。
实际上编译器也提供了默认的复制构造函数,但如果你没有任何字符串到构造函数进行复制,它就可以正常工作。
但是如果你想将字符串从 class 的一个对象复制到 class 的另一个对象,那么你不能将一个内存地址指向两个指针。
尝试做拷贝构造函数的例子
我创建了一个 class,它有一个带参数的构造函数。我认为我在这里需要一个复制构造函数是对的吗?
我有一个class,不接受指点:
class xyz : public Node
{
public:
xyz( uint8_t node);
xyz(const xyz& cxyz);
~xyz();
private:
uint8_t m_node;
uint16_t m_gainxyz;
和基地:
class Node
{
public:
Node();
virtual ~Node();
protected:
std::string m_name;
当我这样做时:
xyz xyz = Initxyz(node);
编译器告诉我创建一个拷贝构造函数。
其中:
xyz PD::Initxyz(Source& inputNode)
{
if (inputNode.getNodeNumber() > 10 )
{
xyz element(inputNode.getNodeNumber());
element.setInputNode(inputNode);
return element;
}
else
{
std::cout << "ERROR IN XYZ CONFIGURATION" << std::endl;
//Throw Exception;
}
}
但是根据我在网上看到的:
If the object has no pointers to dynamically allocated memory, a shallow copy is probably sufficient. Therefore the default copy constructor, default assignment operator, and default destructor are ok and you don't need to write your own.
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html
如果我使用带参数的构造函数,我是否也必须有一个?
我会提供一些规范的参考,以便正式地解释这件事。
如果我正确理解你想要什么,当你需要复制你的对象时 N4296::12.8/1 [class.copy]
:
A class object can be copied or moved in two ways: by initialization (12.1, 8.5), including for function argument passing (5.2.2) and for function value return (6.6.3); and by assignment (5.18).
实际上,复制构造函数可能被隐式声明为已删除,因此如果您在需要复制时尝试调用它,将会出现编译时错误。
相关参考:
An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/move constructor for a class X is defined as deleted (8.4.3) if X has:
(11.1) — a variant member with a non-trivial corresponding constructor and X is a union-like class,
(11.2) — a potentially constructed subobject type M (or array thereof) that cannot be copied/moved because overload resolution (13.3), as applied to M’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor,
(11.3) — any potentially constructed subobject of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or,
(11.4) — for the copy constructor, a non-static data member of rvalue reference type.
其中潜在构造子对象的定义如下N4296::12/5
For a class, its non-static data members, its non-virtual direct base classes, and, if the class is not abstract (10.4), its virtual base classes are called its potentially constructed subobjects.
I have created a class, which has a constructor that takes a parameter. Am I right in thinking that I need a copy constructor here?
您不需要为您的类提供复制构造函数。编译器生成的就足够了。
这里是a working example based on your code:
#include <iostream>
#include <string>
#include <stdint.h>
class Node
{
public:
Node() {}
virtual ~Node() {}
protected:
std::string m_name;
};
class xyz : public Node
{
public:
xyz( uint8_t node) {}
private:
uint8_t m_node;
uint16_t m_gainxyz;
};
xyz Initxyz(Node)
{
return xyz(42);
}
int main()
{
Node node;
xyz xyz = Initxyz(node);
}
当你想做深拷贝而不是浅拷贝时需要复制构造函数copy.It意味着你正在避免悬空指针问题。 实际上编译器也提供了默认的复制构造函数,但如果你没有任何字符串到构造函数进行复制,它就可以正常工作。 但是如果你想将字符串从 class 的一个对象复制到 class 的另一个对象,那么你不能将一个内存地址指向两个指针。 尝试做拷贝构造函数的例子