Skip to main content
gamedev.net
🔒 Locked

Functor Question

Started by Atticus-Atropos 5 replies 800+ views
Original Post
Atticus-Atropos
Atticus-Atropos
I have this code for a functor... it works when I use it the conventional way... but in void main() I'm trying to call the member function with the class name... I can't get it to work. Am I getting the syntax wrong or is it not possible. #include class TX_0 { public: void F(); }; void TX_0::F() { printf("%s\n", __FUNCTION__); } class TX_1 { public: void (TX_0::*FX)(); TX_0 *X; void F_1(); }; void TX_1::F_1() { //All these work... //(X->*FX)(); //(*X.*FX)(); //(this->X->*FX)(); //(*X.*TX_1::FX)(); //(this->X->*TX_1::FX)(); //((*this).X->*TX_1::FX)(); } void main() { TX_0 X_0; TX_1 X_1; X_1.X = &X_0; X_1.FX = &TX_0::F; X_1.F_1(); //this won't work ((X_1).X->*TX_1::FX)(); }
phresnel
phresnel
I am pretty sure that this will help you (for you shalt not name your post "untitled")(vanished). After that, read that, for you shalt not post ill-formatted dumps. Finally, this page can maybe help you, for I have heard your problem has to do with functors.

[Edited by - phresnel on April 1, 2009 7:23:27 AM]
SiCrane
SiCrane
I think you want:
(X_1.X->*X_1.FX)();
Atticus-Atropos
Atticus-Atropos
that does work! I don't understand the syntax though... thanks
visitor
visitor
This is how you call member functions through a member function pointer.

(pointer_to_instance->*pointer_to_member_function)(arguments_if_any);//or(instance.*pointer_to_member_function)(arguments_if_any);
Zahlman
Zahlman
Quote:
Original post by Atticus-Atropos
I have this code for a functor... it works when I use it the conventional way... but in void main()


Do you believe you are writing C, or that you are writing C++?

Because 'void main()' is legal only in the former (and printf() is not good style in the latter), and 'class TX_0' (along with the whole concept of member functions and pointers thereto) only in the latter.

Anyway, it seems your brackets are in the wrong place.

((X_1).X->*TX_1::FX)();


The brackets in bold are useless, because they just wrap a single identifier.

Also, TX_1::FX doesn't make sense: that is requesting the member 'FX' of the class 'TX_1' (i.e. a static member), but FX is a member of instances of TX_1.

You probably wanted:

((X_1.X)->*(X_1.FX))();


See also here.
NotAYakk
NotAYakk
As an aside, edit your post to put the code in [ code ] or [ source ] blocks, and you will save the formatting of the code you copy/paste, and make people happier.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.