C++ 自定义类std::shared\u ptr实例的调用运算符()

C++ 自定义类std::shared\u ptr实例的调用运算符(),c++,c++11,shared-ptr,C++,C++11,Shared Ptr,我有自己的扩展2d数组类CustomDynamicArray,它涵盖std::vector,并允许通过重载运算符通过索引处理其项 直到我有了一个简单的CustomDynamicCarray实例 下一步我可能会处理数组项: _field(x, y) = cell; 或 但是由于我在std::shared\u ptr中包含了我的变量,所以我有一个错误 std::shared_ptr<CustomDynamicArray> _fieldPtr; _fieldPtr(x, y) = cel

我有自己的扩展2d数组类CustomDynamicArray,它涵盖std::vector,并允许通过重载运算符通过索引处理其项

直到我有了一个简单的CustomDynamicCarray实例

下一步我可能会处理数组项:

_field(x, y) = cell;

但是由于我在std::shared\u ptr中包含了我的变量,所以我有一个错误

std::shared_ptr<CustomDynamicArray> _fieldPtr;
_fieldPtr(x, y) = cell; // Error! C2064 term does not evaluate to a function taking 2 arguments
const CustomCell& currentCell = _fieldPtr(x, y); // Error! C2064    term does not evaluate to a function taking 2 arguments
shared_ptr的行为类似于原始指针,您不能像那样直接调用指针上的运算符。您可以取消对std::shared\u ptr的引用,然后调用操作员

或者以丑陋的方式显式调用运算符

_fieldPtr->operator()(x, y) = cell;
const CustomCell& currentCell = _fieldPtr->operator()(x, y);
shared_ptr的行为类似于原始指针,您不能像那样直接调用指针上的运算符。您可以取消对std::shared\u ptr的引用,然后调用操作员

或者以丑陋的方式显式调用运算符

_fieldPtr->operator()(x, y) = cell;
const CustomCell& currentCell = _fieldPtr->operator()(x, y);

您必须首先取消引用指针:

(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);

您必须首先取消引用指针:

(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);

是的,我明白了。这是唯一的方法吗?@MalovVladimir是的,由于智能指针的行为方式与原始指针的行为方式完全相同,因此必须先取消对它的引用,才能获得它所指向的值。这不应该是_fieldPtr->operatorx,y@塔伦,我查过了。我可以使用_newCells->operatorx,y=cell;语法,但我使用重载运算符来简化语法。这个函数调用看起来有点笨拙,但它可以工作。谢谢是的,我明白了。这是唯一的方法吗?@MalovVladimir是的,由于智能指针的行为方式与原始指针的行为方式完全相同,因此必须先取消对它的引用,才能获得它所指向的值。这不应该是_fieldPtr->operatorx,y@塔伦,我查过了。我可以使用_newCells->operatorx,y=cell;语法,但我使用重载运算符来简化语法。这个函数调用看起来有点笨拙,但它可以工作。谢谢谢谢你的回答,草帽鸡谢谢你的回答,草帽鸡
(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);
_fieldPtr->operator()(x, y) = cell;
const CustomCell& currentCell = _fieldPtr->operator()(x, y);
(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);