C++类模板中,如何在特定研究中重载 [ ] 运算符
C++ class template, how to overload [ ] operator in a specific stuation?
我有一个带有非 void 类型参数的类模板
template <typename T, unsigned int n>
class Array
{
private:
T* value;
public:
Array()
{
this->value = new T[n];
}
~Array() {
delete [] this->value;
}
Array(const Array & arr){
this->value = new T[n];
int a = n;
int b = 0;
while(a != 0)
{
this->value[b] = arr.value[b];
b++;
a--;
}
}
Array& operator=(const Array& arr)
{
int a = n;
int b = 0;
while(a != 0)
{
this->value[b] = arr.value[b];
b++;
a--;
}
return *this;
}
T& operator[](int a)
{
return this->value[a];
}
unsigned int size()
{
return n;
}
};
上面是我的类模板,下面是一个名为"Test"的类。
class Test
{
public:
Test() { std::cout << "Test::Test()" << std::endl; }
Test(Test const&) { std::cout << "Test::Test(Test const&)" << std::endl; }
~Test() { std::cout << "Test::~Test()" << std::endl; }
Test& operator=(Test const&)
{
std::cout << "Test& Test::operator=(Test const&)" << std::endl;
return *this;
}
void print() const { std::cout << "Test::print() const" << std::endl; }
void print() { std::cout << "Test::print()" << std::endl; }
};
在我的主文件中.cpp我写了这个
int main(int, char*[])
{
Array<Test, 3> arr_1;
arr_1[1].print();
Test& t1 = arr_1[2];
t1.print();
return 0;
}
我想做的是,
当我打电话给arr_1[1].print();
时,
它必须使用我的"测试"类中的print() const
函数
当我这样做Test& t1 = arr_1[2];
并打电话给t1.print();
,
它必须使用print()
(非常量函数)。
我不明白我如何将 [] 运算符重载到返回一个常量值和一个非常量值。
我的课程模板中是否缺少方法?或者我的重载 [] 运算符实现是错误的?
谢谢!
如果对象上有"const"限定符,则使用"const print"。否则使用其他形式。在您的情况下,没有一个"测试"实例是"const",因此使用非常量版本的打印:
T& operator[](int a)
...
arr[1].print();
Test &t1 = arr[2];
t1.print();
在上面的例子中,arr[1] 是 no-const 的,而 t1 也不是,所以两者都将使用 print 的非常量版本。
但在以下示例中,"t2"将是 const 并将使用 'const 版本的打印函数:
T& operator[](int a)
...
arr[1].print();
const Test &t2 = arr[2];
t2.print();
在这两种情况下,运算符都是非常量。但是,如果您返回"const",则两个变体都将使用打印的"const"版本:
const T& operator[](int a)
...
arr[1].print();
const Test &t1 = arr[2];
t1.print();
好吧,在后一种情况下,将 t1 声明为 non-const 将导致编译失败,因为您尝试将 const 值取消对非常量值的引用。
你可以Array::operator[]
返回一个const T&
,这将强制从返回的引用中调用print
的const
版本。但在这种情况下,行Test& t1 = arr_1[2];
将失败,因为您无法在没有 const_cast
的情况下将常量引用分配给非常量引用。
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 在 myVector 类中重载运算符 + 时出错
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 如何在 cpp 中重载运算符 +=?
- C++ 如何重载 [] 运算符并进行函数调用
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 重载运算符有地址吗?
- 如何迭代重载运算符 [] 的类?
- 重载运算符与添加问题
- 模板基类中的重载运算符
- 如何调用用于重载运算符"<<"的 friend 函数?
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- 重载运算符<<采用谷歌 C++ 风格
- C++ 如何正确重载 + 运算符
- cout (<<) 重载运算符不打印减去的矩阵
- 如何在 c++ 中重载运算符 + 以便能够 whrite c_str = "smth" + c_str;
- 重载运算符*以获取对另一个类的实例的引用