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!

Answers


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()();

Need Your Help

array of special characters to replace in a text, php, json

php arrays json

I have a huge text that i keep getting in a json format. When i receive them in json, for some special characters like ' " &amp;copy, i receive them differently. i am using php and json to convert ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.