繁体   English   中英

为自定义类std :: shared_ptr实例调用Operator()

[英]Call Operator() for custom class std::shared_ptr instance

我有自己的扩展2D数组类CustomDynamicArray ,它覆盖std::vector并允许通过重载的operator()通过索引处理其项。

CustomCell& CustomDynamicArray::operator()(size_t colIdx, size_t rowIdx)

直到我有简单的CustomDynamicArray实例

CustomDynamicArray _field;

我可能会以其他方式处理数组项:

_field(x, y) = cell;

要么

const CustomCell& currentCell = _field(x, y);

但是因为我将我的变量覆盖到std::shared_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

我应该如何解决此编译错误?

现在,我看到使用此语法的唯一方法:

(*_newCells)(x, y) = cell;

std::shared_ptr智能指针 ,其行为类似于原始指针,您不能像这样直接在指针上调用operator() 您可以在std::shared_ptr上取消引用,然后调用operator()

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

或显式调用丑陋的operator() )。

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

您必须先取消引用指针:

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

暂无
暂无

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

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