简体   繁体   English

C ++重载括号[]运算符get&set不同的返回类型

[英]C++ overloading bracket [] operator get & set with different return types

I'm trying to overload the [] operator for a class that I created with different return types for the getter and setter. 我正在尝试为我创建的类重载[]运算符,该类使用getter和setter的不同返回类型。

I want the setter to return a reference to a another class instance, which is happening fine at the moment, the problem is that I want the getter to return a char, but in my main when i'm assigning char c = object[5] it's calling the setter instead of the getter and returning the wrong return type. 我希望设置器返回对另一个类实例的引用,此举目前还很好,问题是我希望getter返回一个字符,但是在我的主对象中,当我分配字符c = object [5]时]它将调用设置方法而不是获取方法,并返回错误的返回类型。

That how it looks in my code: 那在我的代码中看起来如何:

Board.h Board.h

const char& operator[](Board_Node index) const;
Board_Node& operator[](Board_Node index);

Board.cpp Board.cpp

const char& Board::operator[](Board_Node index) const
{
    int boardIndex = index.i * _boardSize + index.j;
    if (boardIndex < 0 || boardIndex >= _board.size())
    {
        throw IllegalCoordinateException(index.i, index.j);
    }
    return _board[index.i * _boardSize + index.j].token;
}

Board_Node & Board::operator[](Board_Node index)
{
    int boardIndex = index.i * _boardSize + index.j;
    if (boardIndex < 0 || boardIndex >= _board.size())
    {
        throw IllegalCoordinateException(index.i, index.j);
    }
    return _board[index.i * _boardSize + index.j];
}

main.cpp main.cpp中

char c = board1[{1, 2}]; cout << c << endl;

That line results in error: no suitable conversion function from "Board_Node" to "char" exists. 该行导致错误:不存在从“ Board_Node”到“ char”的合适转换函数。

Already tried all forms with const everywhere and nothing worked. 已经在所有地方使用const尝试了所有形式,但没有任何效果。

Appericiate any help, thank you! 请给予任何帮助,谢谢!

Calling the overloaded functions getters and setters based on whether an object is const or non- const is not appropriate. 根据对象是const还是非const调用重载的函数getter和setter是不合适的。

The non- const version of the overload is given higher priority if the object is not const . 如果对象不是const ,则将为重载的非const版本赋予更高的优先级。

I suggest adding an explicitly named getter function, which can use the overloaded operator[] function. 我建议添加一个显式命名的getter函数,该函数可以使用重载的operator[]函数。

char get(Board_Node index) const
{
   return (*this)[index];
}

As a matter of good practice, it will be better to change the return type of the const version of the operator[] function to return Board_Node const& . 作为一种好的做法,最好将operator[]函数的const版本的返回类型更改为return Board_Node const&

Board_Node const& operator[](Board_Node index) const;
Board_Node& operator[](Board_Node index);

That will allow you to extract other information from the corresponding Board_Node , not just a char . 这样您就可以从相应的Board_Node提取其他信息,而不仅仅是char

With that, you won't need the 'get function. You'll have to change usage of the 这样,您将不需要'get function. You'll have to change usage of the function. You'll have to change usage of the operator[]` function a bit. function. You'll have to change usage of the稍微function. You'll have to change usage of the operator []`函数的function. You'll have to change usage of the

char c = board1[{1, 2}].token;
cout << c << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM