当类用作映射值时,复制构造函数不起作用
copy constructor doesn't work when the class is used as a map value
我找不到一个更简单的方法来解释我的问题,而不是直接粘贴在这里(一个简化的版本)。我有一个模板化类与必要的赋值运算符,默认构造函数和common
复制构造函数。当我尝试在我的代码中使用该类时,我得到如下错误:
#include<map>
#include<vector>
template<class T>
class BufferContainer
{
public:
BufferContainer& operator=(BufferContainer& other)
{
buffer = other.get();
return *this;
}
BufferContainer( const BufferContainer& other ) :
buffer( other.get() )
{
}
BufferContainer(){
}
std::vector<T>& get() {
return buffer;
}
void add(T value) {
buffer.push_back(value);
}
std::vector<T> buffer;
};
int main()
{
std::map<int, BufferContainer<int> > myMap;
myMap[1].add(1);
return 1;
}
,错误是:
Practice $ g++ template.cpp
template.cpp: In instantiation of ‘BufferContainer<T>::BufferContainer(const BufferContainer<T>&) [with T = int; BufferContainer<T> = BufferContainer<int>]’:
/usr/include/c++/4.7/bits/stl_pair.h:105:31: required from ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const int; _T2 = BufferContainer<int>]’
/usr/include/c++/4.7/bits/stl_map.h:458:11: required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = BufferContainer<int>; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, BufferContainer<int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = BufferContainer<int>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
template.cpp:38:9: required from here
template.cpp:16:26: error: passing ‘const BufferContainer<int>’ as ‘this’ argument of ‘std::vector<T>& BufferContainer<T>::get() [with T = int]’ discards qualifiers [-fpermissive]
如果你帮我解决这个问题的方法,我会很感激,更重要的是,告诉我为什么我得到这个错误。由于
您的get()
函数不是const
限定的,并且您正在通过复制构造函数中对const
的引用来调用它:
BufferContainer( const BufferContainer& other ) :
// ^^^^^
buffer( other.get() )
// ^^^^^^^^^^^
{
}
这就是编译器报错的原因。不能通过引用const
来调用非const
函数。引用const
意味着您不打算修改被引用对象的状态,因此您只能调用承诺不修改对象状态的函数。这就是成员函数上的const
限定符的作用——做出这样的承诺。
get()
应该限定为const
,并返回对const
向量的引用:
std::vector<T> const& get() const
// ^^^^^ ^^^^^
{
return buffer;
}
如果你需要你的非const
函数get()
,因为你想让客户端修改内部的buffer
(建议:考虑这是否真的是一个好主意),那么你将不得不提供两个 get()
的重载:
- 一个
const
限定的,返回对const
向量的引用(如上所示) - 非
const
限定的,返回对可修改向量(如原始get()
)的引用
这意味着您应该提供get()
方法的const
版本:
const std::vector<T>& get() const
{
return buffer;
}
,因为复制构造函数正确地接受const
引用:
BufferContainer( const BufferContainer& other )
这意味着你只能在other
上调用const
方法。
注意返回类型是const
版本,get()
是const
引用。
取other
作为const
的引用,get
为非const方法。不能从const引用中调用非const方法
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 复制构造函数、赋值运算符C++
- std::ofstream 作为类成员删除复制构造函数?
- 复制构造函数C++无法正确复制指针
- 关于复制构造函数的一个棘手问题
- 为什么调用复制构造函数而不是移动构造函数?
- 填充上编译器生成的复制构造函数之间的不一致
- C++ 对象指针数组的复制构造函数
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 防止在复制构造函数中隐式调用基构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 具有已删除移动和复制构造函数的类的就地构造
- 复制构造函数隐式转换问题
- 复制构造函数中的递归调用