为动态字符串数组创建一个不带=赋值的Copy函数
C++ - Creating a Copy function without = assignment for a Dynamic String Array
试图为动态分配的数组编写复制函数。
在我的头文件中我有:
#include <memory>
#include <string>
using std::string;
using std::unique_ptr;
using std::make_unique;
class StringSet{
public:
//create an empty set
StringSet() = default;
StringSet(int capacity);
//copy a set
StringSet(const StringSet&);
StringSet& operator[](const int);
//Insert a string to the set
bool insert(string);
//Remove a string from the set
bool remove(string);
//Test whether a string is in the set
int find(string) const;
//Get the size of the set
int size() const;
//get string at position i
string get(int i) const;
//Return the set union of the set and another StringSet
StringSet setunion(const StringSet&) const;
//Return the intersection of the set and another StringSet
StringSet intersection(const StringSet&) const;
//Return the set diffference of the set and another StringSet
StringSet difference(const StringSet&) const;
//prevent default copy assignment
StringSet& operator=(const StringSet&) = delete;
int NOT_FOUND = -1;
static constexpr int def_capacity {4};
private:
int arrSize {def_capacity};
int currentSize {0};
unique_ptr<string[]> arr {make_unique<string[]>(def_capacity)};
};
在我的实现文件中我有:
#include "StringSet.h"
#include <iostream>
#include <utility>
StringSet::StringSet(int capacity)
: arrSize{capacity},
arr{make_unique<string[]>(capacity)}
{
}
StringSet::StringSet(const StringSet& a)
{
auto a2 = StringSet(currentSize);
for (auto i=0; i < currentSize ; i++ )
{
a2[i] = a[i];
}
}
编译错误:
error: constructors may not be cv-qualified
error: no match for 'operator=' (operand types are 'StringSet' and 'std::string {aka std::basic_string<char>}')
error: passing 'const StringSet' as 'this' argument discards qualifiers [-fpermissive]
error: use of deleted function 'StringSet& StringSet::operator=(const StringSet&)'
我的赋值重载了赋值操作符=,因此我不能在这里使用它。有没有另一种不使用赋值操作符实现复制函数的方法——std::string中是否有任何东西允许我们以这种方式更容易地复制内容?
如果还有什么需要补充的,请告诉我。
谢谢。
这段代码的问题:
StringSet::StringSet(const StringSet& a)
{
auto a2 = StringSet(currentSize);
for (auto i=0; i < currentSize ; i++ )
{
a2[i] = a[i];
}
}
是,即使它被编译了,你也从来没有真正初始化this
的成员…您正在初始化一些在构造函数末尾超出作用域的临时a2
。你需要:
StringSet::StringSet(const StringSet& a)
: StringSet(a.arrSize)
{
currentSize = a.currentSize;
for (auto i=0; i < currentSize; i++ )
{
arr[i] = a.arr[i];
}
}
另外,您的operator[]
返回StringSet&
,它可能应该返回std::string&
。
同样,您应该避免像您正在做的那样将名称引入全局命名空间。保持本地化。编写std::
不是负担。
相关文章:
- gtest_使用setargpointee在函数中赋值
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 全局作用域中函数指针的赋值
- 关于 c++ 函数中指针赋值的简单问题
- 复制构造函数、赋值运算符C++
- 复制包含C++所有元素的对象!(构造函数和赋值,最佳实践?
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- C++矢量复制构造函数和赋值运算符是否也复制保留空间?
- 复制赋值函数如何访问另一个对象的私有成员(Stroustroup 原则和实践书)?
- 为什么我不能为变量赋值函数?
- C++标准是否对向量赋值函数或构造函数提出了明确的要求
- 为什么当复制赋值函数不返回任何内容时编译器不引发错误?
- 为什么当定义析构函数时,编译器不再添加默认的move函数和赋值函数
- 编译器为具有引用和常量成员的类生成了复制/赋值函数
- 添加unique_ptr作为类的实例字段,而不是显式地删除复制/赋值函数
- c++只赋值函数调用返回的结构体的一部分