Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ operator overload ->


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Prozak   Members   -  Reputation: 869

Like
0Likes
Like

Posted 26 April 2013 - 12:56 PM

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.addMethod( "nameHere" );

x.addMethod( "setAge" );

 

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!

 

 

 

 

 

 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8089

Like
3Likes
Like

Posted 26 April 2013 - 01:53 PM

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.



#3 AllEightUp   Moderators   -  Reputation: 4195

Like
3Likes
Like

Posted 26 April 2013 - 08:48 PM

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.)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS