This topic is 1879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

I'm back to coding in C++, after a somewhat long stint coding PHP.

I'm rewriting some code, and the way I'd like to deal with it, would be to overload the -> operator, so that I can "call" arbitrary method names, that are assigned at run time by various plugins and libraries that dynamically link to the process.

From what I know, this isn't possible, because the -> operator has to return the function to handle the call, and it does not give us the name of the method...

In pseudo-code:

myClass X;

x->setAge( 25 );

- - -

The code, again in pseudo-code:

int operator ->( char* methodName )

{

if( methodName in listOfRegisteredMethods )

{

return methodFunctionPointer();

}

else

{

throw Exception...

};

};

- - -

I know that there have been, recently, changes and updates to the language. I've written myself a lambda function this past week for the very first time. I would just like to know if anything has been added that would allow me to accomplish something like the above.

Thanks for any replies!

##### Share on other sites

You cannot have any parameters to operator ->. It's just a function that returns the next pointer to apply the right hand side of the expression on.

myClass x;
x->setAge(25);


What this does (when correctly implemented, that is) is call myClass::operator->(void) that returns a pointer to an object of a different type, and the setAge member function of that object is called. If the object returned has its operator -> overloaded, then the operator is called again, and again, and so on, until you get to an object that doesn't have it overloaded.

What it appears to me that you want to do; adding functions of a class dynamically, is not possible.

##### Share on other sites

I'd argue that it is possible but it takes a rethink.  This is basically just local remote procedure call (command pattern locally) and has many uses.  With the addition of function, bind and tuple in C++11, the concept is possible but the conventions need to be modified.  I can't include all the code here, it takes forever (and it's tricky template stuff I'm sure to get wrong the first couple writings) to write out, but the idea's are viable.

First change: don't use -> and replace it with ().  I.e. x->setAge( 25 ); becomes x( setAge, 25 );.  Given this is a "dynamic" member, it makes sense that it would be a command style pattern instead of a de-reference pattern.  In order to make local members conform, you have to register them in the same manner as external functions.

Your 'registered' function will have something like the form of:

typedef std::function< void ( std::tuple& ) >  RegisteredFunction_t;

You have to apply a composite bind to build the function object passed into the registration though, that's pretty complicated unfortunately and I'd probably make a hash of any example, but it is possible and I've done it in some complicated code.  (Though I was using Boost function/bind at the time.)

Finally, given the name of the function you end up with a map of names to function objects which will process the tuples.  You can make a reference object to stand in for the name or just use the string, i.e. "obj( refFunc, ...args )" or "obj( "AStringName", ... args )".

Why I know this is possible is that I had a really complicated XML system where some of the sections were completely under plugin control and this is basically how it worked.

This is a horribly high level overview but it is possible.  Explore function, bind and tuple and you should be able to figure it all out.  (NOTE: bind will require composite binds which are really confusing at first, stick with it though, they make so many things really easy.)

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999554
×