3

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!

4

1 回答 1

10

最简单和最清晰的方法是取消引用指针:

(*j)();

或者,您可以使用->带有函数名称的语法(即operator()):

j->operator()();
于 2013-05-03T00:09:56.003 回答