c++ - 无法将此指针从 const Class<T> 转换为 Class<T>&

标签 c++ reference constants copy-constructor

我正在尝试实现一个简单的复制构造函数:

template<typename T>
MyClass<T>::MyClass(const MyClass<T> &other) {
  MyIterator<T> it = other.begin();
  //...
};

成员函数主体中的那一行会产生此错误:

Cannot convert this pointer from const Class to Class&

我尝试了 const_cast 的东西,但没有成功。

最佳答案

您的 begin 方法显然是非 const 方法,但您尝试在 const 对象上调用它。

关于c++ - 无法将此指针从 const Class<T> 转换为 Class<T>&,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24391973/

相关文章:

c++ - 我什么时候在 C++ 中使用 const volatile、register volatile、static volatile?

用于信号处理的 C++ 库

C++指针引用方法调用

c++ - C++ 是否支持与 STL 兼容的不可变记录类型?

c++ - 在 C++ 中编写简单的 get/set 方法时,是否应该始终使用 const 引用作为参数?

c++ - 我们可以在 constexpr 函数中省略局部变量的 const 吗?

c++ - 什么是 SDL_Joystick,什么是 SDL_GameController?两者之间有什么关系?

c++ - KDevelop 中的多行编辑?

c++ - 取消引用函数参数中的引用

c++ - 在 C++ 中,如果 "int a = 3; int* p = &a;",那么为什么 "const int* &pp = p"是不允许的,而 "const int* const &pp = p"是允许的?