Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Operator() for std::shared_ptr instance

CustomDynamicArray class wraps std::vector and gives an access to its items by pair of indexes via overloaded operator operator()

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

It makes possible to address an array items a natural way:

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

But since I covered my variable into std::shared_ptr I have an error

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

How can I fix this compile error? Now I can see only way to use this syntax:

(*_newCells)(x, y) = cell;
like image 297
Malov Vladimir Avatar asked May 11 '26 22:05

Malov Vladimir


2 Answers

std::shared_ptr is smart pointer which behaves like raw pointers, you can't call operator() on the pointer directly like that. You could dereference on the std::shared_ptr then call the operator().

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

Or invoke operator() explicitly (in ugly style).

_fieldPtr->operator()(x, y) = cell;
const CustomCell& currentCell = _fieldPtr->operator()(x, y);
like image 111
songyuanyao Avatar answered May 16 '26 09:05

songyuanyao


You have to deref the pointer first:

(*_fieldPtr)(x, y) = cell;
const CustomCell& currentCell = (*_fieldPtr)(x, y);
like image 43
Hatted Rooster Avatar answered May 16 '26 10:05

Hatted Rooster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!