Reputation: 1837
Given a class Foo
with overloaded Boo* operator()(unsigned int)
, how should the overloaded operator be accessed?
I originally tried
std::unique_ptr<Foo> foo_ptr(new Foo);
Boo* boo_ptr = foo_ptr(1);
But this doesn't work, so I tried:
std::unique_ptr<Foo> foo_ptr(new Foo);
Boo* boo_ptr = foo_ptr->(1);
but this doesn't work either (and I didn't really expect it to). Instead I have to do
std::unique_ptr<Foo> foo_ptr(new Foo);
Boo* boo_ptr = (*foo_ptr)(1);
Which is more verbose than declaring a method in Foo
such as ByIndex(unsigned int)
instead (and making it shorter and more succinct was why I overloaded the operator in the first place).
Is there a way to do this without having to use the *
operator to get the base value of the pointer?
Upvotes: 0
Views: 47
Reputation: 38218
One way is to make a reference:
std::unique_ptr<Foo> foo_ptr(new Foo);
Foo& foo_ref = *foo_ptr;
Boo* boo_ptr = foo_ref(1);
Besides that, I think Mike Seymour is correct that there's nothing else you can do to invoke operator()
.
Upvotes: 2
Reputation: 254471
No, you have to dereference the pointer to access the object; so the only ways to call the operator are
(*foo_ptr)(1)
foo_ptr->operator()(1);
Upvotes: 4