llk
llk

Reputation: 2561

Calling overloaded operator function from struct pointer

I have the following struct in C++

struct Jam
{
    void operator()()
    {
        cout << "Test";
    }
};

And I am able to call the overloaded function like so:

Jam j;
j();

But I was wondering what the proper way to call the function from a pointer to the same struct. For example if I have:

Jam *j = new Jam;
j->();

I receive errors telling me it needs a function name. Is this possible? Thanks!

Upvotes: 3

Views: 1362

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

The easiest and clearest way is to dereference the pointer:

(*j)();

Alternatively, you can use the -> syntax with the function's name (which is operator()):

j->operator()();

Upvotes: 10

Related Questions