member function calling member function through pointer

member function calling member function through pointer

Post by Ad Lagendi » Fri, 13 Aug 2004 05:20:16


I want to call from inside a member function another
class member function through a member function pointer
(without having to create an instance of the class)
However the following class definition does not
compile (Borland).
Error: calling of nonfunction.
Playing around with * and & doesnot help.
I am probably doing something wrong.
Ad


class A {
typedef int (A::*Ptr)();
Ptr ptr_;
int F1() {return 1;}
void F2() {
int i = F1();
ptr_ = &A::F1;
int j = ptr_(); // error calling of nonfunction.
}
};



[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function calling member function through pointer

Post by Ali Cehrel » Fri, 13 Aug 2004 21:03:26


> I want to call from inside a member function another class member
> function through a member function pointer (without having to create an
> instance of the class) However the following class definition does not
> compile (Borland).
> Error: calling of nonfunction.

> class A {
[...]
> ptr_ = &A::F1;
> int j = ptr_(); // error calling of nonfunction.
> }
> };

Pointer to member function must be called through an instance. If you
want to call it on this object, try

int j = (this->*ptr_)();

Ali

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
 
 

member function calling member function through pointer

Post by Victor Baz » Fri, 13 Aug 2004 21:03:54


> I want to call from inside a member function another
> class member function through a member function pointer
> (without having to create an instance of the class)
> However the following class definition does not
> compile (Borland).
> Error: calling of nonfunction.
> Playing around with * and & doesnot help.
> I am probably doing something wrong.
> Ad
>
>
> class A {
> typedef int (A::*Ptr)();
> Ptr ptr_;
> int F1() {return 1;}
> void F2() {
> int i = F1();
> ptr_ = &A::F1;
> int j = ptr_(); // error calling of nonfunction.

You have to use the special syntax here:

int j = (this->*ptr_)();

> }
> };

Victor

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function calling member function through pointer

Post by Edward Die » Fri, 13 Aug 2004 21:11:38


> I want to call from inside a member function another
> class member function through a member function pointer
> (without having to create an instance of the class)
> However the following class definition does not
> compile (Borland).
> Error: calling of nonfunction.
> Playing around with * and & doesnot help.
> I am probably doing something wrong.
> Ad
>
>
> class A {
> typedef int (A::*Ptr)();
> Ptr ptr_;
> int F1() {return 1;}
> void F2() {
> int i = F1();
> ptr_ = &A::F1;
> int j = ptr_(); // error calling of nonfunction.

int j = (this -> *ptr_)();

Pointer to member functions can only be called by being bound to an object
of the correct class. You could also write:

int j = ((*this).*ptr_)();

as an alternative syntax in this case but clearly the first form is better
when you already have a pointer to the object itself in the 'this' variable.

> }
> };


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function calling member function through pointer

Post by Ulrich Eck » Sat, 14 Aug 2004 19:53:56


> I want to call from inside a member function another
> class member function through a member function pointer
> (without having to create an instance of the class)

*BEEEP*
Wrong, what you want can't be possible. Please read the FAQ which explains
the difference between a function and a memberfunction.

> class A {
> typedef int (A::*Ptr)();
> Ptr ptr_;
> int F1() {return 1;}
> void F2() {
> int i = F1();
> ptr_ = &A::F1;
> int j = ptr_(); // error calling of nonfunction.
> }
> };

Looking at F1, I see that it doesn't touch any of A's members. If that is to
remain so, you can make it a normal function (e.g. by making it
class-static). Else, you need to use memberfunction pointers(and then with
an object).

Not sure if that helps, but take a look at boost function and bind. It's by
far better than function pointers in some cases.

Uli

--
FAQ: http://www.yqcomputer.com/ ++-faq-lite/

/* bittersweet C++ */
default: break;

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function calling member function through pointer

Post by jhopki » Sat, 14 Aug 2004 20:03:00


> I want to call from inside a member function another
> class member function through a member function pointer
> (without having to create an instance of the class)
> However the following class definition does not
> compile (Borland).
> Error: calling of nonfunction.
> Playing around with * and & doesnot help.
> I am probably doing something wrong.
> Ad
>
>
> class A {
> typedef int (A::*Ptr)();
> Ptr ptr_;
> int F1() {return 1;}
> void F2() {
> int i = F1();
> ptr_ = &A::F1;
> int j = ptr_(); // error calling of nonfunction.
> }
> };
>

Do you mean

int j = (this->*ptr_)();

?

Non-static member functions must always be called on a specific object.

James

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function calling member function through pointer

Post by Catalin Ma » Sat, 14 Aug 2004 20:03:43

"Ad Lagendijk" < XXXX@XXXXX.COM > writes:
> I want to call from inside a member function another
> class member function through a member function pointer
> (without having to create an instance of the class)
> However the following class definition does not
> compile (Borland).
> Error: calling of nonfunction.
> Playing around with * and & doesnot help.
> I am probably doing something wrong.

It would be nicer to have a base class with a pure virtual function F1
and just derive from it and implement this function.

> class A {
> typedef int (A::*Ptr)();
> Ptr ptr_;
> int F1() {return 1;}
> void F2() {
> int i = F1();
> ptr_ = &A::F1;
> int j = ptr_(); // error calling of nonfunction.

Anyway, in case you still want this, try:

int j = (this->*ptr_)();

Not being a static function, the (*ptr_)() function needs "this" to be
passed.

Catalin

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function calling member function through pointer

Post by jhopki » Sat, 14 Aug 2004 23:30:30


Careful: no space allowed in ->* - it's a single operator


James

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function calling member function through pointer

Post by devnul » Sun, 15 Aug 2004 01:47:00


> I am probably doing something wrong.

Yes

> class A {
> typedef int (A::*Ptr)();
> Ptr ptr_;
> int F1() {return 1;}
> void F2() {
> int i = F1();
> ptr_ = &A::F1;
> int j = ptr_(); // error calling of nonfunction.
int j = (this->*ptr_)();
> }
> };

--
Alan Griffiths
http://www.yqcomputer.com/
Mobile: +44 (0)798 9938 758

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]