重载的复制构造函数不';好像没人叫你 我对C++非常陌生,我正试图为我的哈希表类重载复制构造函数。我已经搜索了好几个小时,但似乎不明白为什么它没有被调用。我正在使用MVS 2015。相关代码: class HashingTable { ... public: /* * Constructors */ // Default constructor HashingTable(); // Default destructor virtual ~HashingTable(); // Constructor requiring size of vector to create HashingTable(int size); // Copy constructor requiring another instance of HashingTable explicit HashingTable(const HashingTable& ht); } // Copy constructor using another table instance template <class obj_type> HashingTable<obj_type>::HashingTable(const HashingTable& ht) { cout << "Copy constructor called" << endl; *this = ht; } // Overload of operator= template <class obj_type> HashingTable<obj_type>& HashingTable<obj_type>::operator=(constHashingTable& ht) { cout << "In operator=..." if (this != &ht) // no need to copy itself { // do some stuff to *this } return *this; } 类哈希表 { ... 公众: /* *建设者 */ //默认构造函数 HashingTable(); //默认析构函数 虚拟~HashingTable(); //需要向量大小才能创建的构造函数 哈希表(int-size); //需要另一个HashingTable实例的复制构造函数 显式哈希表(常量哈希表&ht); } //使用另一个表实例复制构造函数 模板 HashingTable::HashingTable(常量HashingTable&ht) { cout

重载的复制构造函数不';好像没人叫你 我对C++非常陌生,我正试图为我的哈希表类重载复制构造函数。我已经搜索了好几个小时,但似乎不明白为什么它没有被调用。我正在使用MVS 2015。相关代码: class HashingTable { ... public: /* * Constructors */ // Default constructor HashingTable(); // Default destructor virtual ~HashingTable(); // Constructor requiring size of vector to create HashingTable(int size); // Copy constructor requiring another instance of HashingTable explicit HashingTable(const HashingTable& ht); } // Copy constructor using another table instance template <class obj_type> HashingTable<obj_type>::HashingTable(const HashingTable& ht) { cout << "Copy constructor called" << endl; *this = ht; } // Overload of operator= template <class obj_type> HashingTable<obj_type>& HashingTable<obj_type>::operator=(constHashingTable& ht) { cout << "In operator=..." if (this != &ht) // no need to copy itself { // do some stuff to *this } return *this; } 类哈希表 { ... 公众: /* *建设者 */ //默认构造函数 HashingTable(); //默认析构函数 虚拟~HashingTable(); //需要向量大小才能创建的构造函数 哈希表(int-size); //需要另一个HashingTable实例的复制构造函数 显式哈希表(常量哈希表&ht); } //使用另一个表实例复制构造函数 模板 HashingTable::HashingTable(常量HashingTable&ht) { cout,c++,operator-overloading,C++,Operator Overloading,myHashTable属于HashingTable*类型。这里重要的是它是一个指针,而不是一个对象 table2也是一个HashingTable*,因此当您执行HashingTable*table2(myHashTable);时,指针值会被复制,而HashingTable的实际复制构造函数永远不会被调用 您必须复制一个哈希表对象,才能调用您声明的构造函数,即 HashingTable<char*> notAPointerHashTable(*myHashTable); Hashin

myHashTable
属于
HashingTable*
类型。这里重要的是它是一个指针,而不是一个对象


table2
也是一个
HashingTable*
,因此当您执行
HashingTable*table2(myHashTable);
时,指针值会被复制,而
HashingTable
的实际复制构造函数永远不会被调用

您必须复制一个
哈希表
对象,才能调用您声明的构造函数,即

HashingTable<char*> notAPointerHashTable(*myHashTable);
HashingTable notAPointerHashTable(*myHashTable);

myHashTable
属于
HashingTable*
类型。这里重要的是它是一个指针,而不是一个对象


table2
也是一个
HashingTable*
,因此当您执行
HashingTable*table2(myHashTable);
时,指针值会被复制,而
HashingTable
的实际复制构造函数永远不会被调用

您必须复制一个
哈希表
对象,才能调用您声明的构造函数,即

HashingTable<char*> notAPointerHashTable(*myHashTable);
HashingTable notAPointerHashTable(*myHashTable);

哈希表*表2(myHashTable)
声明指向
哈希表
的指针,而不是实际的
哈希表
对象。我将使用
int
显式构造函数,而不是复制构造函数…
哈希表
不是模板,因此您无法编写
模板哈希表
。您的代码也不会编译缺少分号等。请发布。
HashingTable*table2(myHashTable)
声明指向
哈希表
的指针,而不是实际的
哈希表
对象。我将使用
int
显式构造函数,而不是复制构造函数…
哈希表
不是模板,因此您无法编写
模板哈希表
。您的代码也不会编译缺少分号等。请发布。非常棒的解释,Rakete!非常感谢。@user3327134没问题:)非常棒的解释,Rakete!非常感谢。@user3327134没问题:)