简体   繁体   English

更改默认的复制构造函数C ++

[英]Changing the default copy constructor C++

I am having problems understanding how to override the default copy constructor in C++. 我在理解如何覆盖C ++中的默认副本构造函数时遇到问题。 I am receiving no compilation errors. 我没有收到任何编译错误。 The previous examples show to me our of the pattern below. 前面的示例向我展示了我们的以下模式。 Below are listed excerpts from the files HashTable.cpp and Hashtable.h . 以下是文件HashTable.cppHashtable.h摘录。

Hashtable.h 哈希表

HashTable& operator=(const HashTable& other);`

HashTable.cpp 哈希表

const HashTable& HashTable::operator=(const HashTable& other) {
    std::cout << "EQUAL OPERATOR METHOD" << std::endl;
    return *this;
}

main.cpp main.cpp

HashTable ht1 {9};
HashTable ht2 { ht1 };

Although when I compile, it appears as if the copy constructor is not called. 尽管在编译时,好像没有调用复制构造函数。 To clarify, I am trying to copy one variable to the other. 为了澄清,我试图将一个变量复制到另一个变量。

It is worth noting that I am coding in c++11 on Ubuntu 14.04. 值得注意的是,我正在Ubuntu 14.04上使用c ++ 11进行编码。 As coding c++ in Ubuntu has already had many hangups for me, I am uncertain if this is c++ or ubuntu problem. 由于在Ubuntu中编写c ++对我来说已经有很多麻烦,所以我不确定这是c ++还是ubuntu问题。 I have spent quite some time trying to figure out what is going on here so please do not down vote. 我花了很多时间试图弄清楚这里发生了什么,所以请不要投反对票。

The code you wrote above is the overriding of copy assignment operator , but, according to your main.cpp , seems like you need the copy constructor (don't be scared by the amount of text in these descriptions, it's really easy to understand). 上面编写的代码是copy assignment operator的重载,但是,根据main.cpp看来,您似乎需要copy constructor (不必担心这些描述中的文本数量,这很容易理解) 。 Try the following code: 尝试以下代码:

HashTable.h 哈希表

class HashTable
{
private:
    // private members declaration...
public:
    // public members declaration...
    HashTable(const HashTable& other);
}

HashTable.cpp 哈希表

// Copy constructor implementation
HashTable::Hashtable(const HashTable& other){
    // implement your own copy constructor
    std::cout << "OVERRIDED COPY CONSTRUCTOR METHOD" << std::endl;
    // This is the constructor, so don't have to return anything
}

main.cpp main.cpp

HashTable ht2(ht1);

PS: Not sure about HashTable ht2 {ht1} (using the symbols { and } ). PS:不确定HashTable ht2 {ht1} (使用符号{} )。 Seems like it's C++14 feature, according to the comment of MM . 根据MM的评论,似乎是C++14功能。

First of all, the sample code in your question is a copy assignment. 首先,问题中的示例代码是副本分配。 The difference between the copy constructor and the copy assignment is that, the copy construction can only be called when initializing an object. 复制构造函数和复制分配之间的区别在于,复制构造只能在初始化对象时调用。 After an object is initialized and you want to pass another initialized object to it, the copy assignment is rather called. 初始化对象后,您想将另一个初始化的对象传递给它,则将调用复制分配。 So in the main function since ht1 was passed to ht2 while initializing it, it will rather call the copy constructor. 因此,在主函数中,因为在初始化ht2时将ht1传递给ht2 ,所以它宁愿调用复制构造函数。 But in your code, you defined the copy assignment and not the copy constructor. 但是在您的代码中,您定义了副本分配,而不是副本构造函数。 check out copy assignment c++ for more details about the difference between the copy assignment and the copy constructor. 请查看副本分配c ++,以获取有关副本分配和副本构造函数之间差异的更多详细信息。

HashTable::operator=(const HashTable& other) is assignment opperator. HashTable :: operator =(const HashTable&other)是赋值运算符。 Copy constructor should be written as HashTable::HashTable(const HashTable& other). 复制构造函数应写为HashTable :: HashTable(const HashTable&other)。

" HashTable ht2 { ht1 } " is not invoking copy-constructor, it's actually invoking initializer_list: " HashTable(initializer_list< HashTable>) " HashTable ht2 {ht1} ”没有调用复制构造函数,它实际上是在调用initializer_list:“ HashTable(initializer_list <HashTable>)

To invoke copy constructor you should write HashTable hash(anotherHashTable) in your main.cpp. 要调用复制构造函数,您应该在main.cpp中编写HashTable哈希(anotherHashTable)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM