Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to call method of the class which contains a pointer to other class method?

Reply
Thread Tools

how to call method of the class which contains a pointer to other class method?

 
 
Pawel_Iks
Guest
Posts: n/a
 
      07-30-2007
I have following class definition (A.h file):

class A
{
public:
int N;
int count1(int n) {n++;}
int count2(int n) {n+=5;}
int otherFun(int n, int (*fun)(int));
}

and I want to implement otherFun method (in A.cpp file):

int A:therFun(int,int (*fun)(int))
{
int c=0,d;
d=fun(c);
}

and call it (in main.cpp):

int main()
{
A obj=A();
int t;
A.N=12;
t=A.otherFun(N,test.count1);
return 0;
}

and it doesn't work ... I have two questions:
1) If there implementation of otherFun method is correct, and when
it's correct how can i call this method in main() function?
2) why it doesn't work? (for simply function - not class member - it
works fine)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-30-2007
Pawel_Iks wrote:
> I have following class definition (A.h file):
>
> class A
> {
> public:
> int N;
> int count1(int n) {n++;}
> int count2(int n) {n+=5;}
> int otherFun(int n, int (*fun)(int));
> }
>
> and I want to implement otherFun method (in A.cpp file):
>
> int A:therFun(int,int (*fun)(int))
> {
> int c=0,d;
> d=fun(c);
> }
>
> and call it (in main.cpp):
>
> int main()
> {
> A obj=A();
> int t;
> A.N=12;
> t=A.otherFun(N,test.count1);


The usual beginner's mistake: a non-static member function is NOT
compatible with a pointer to function. Look up and read about
"pointer to member".

> return 0;
> }
>
> and it doesn't work ... I have two questions:
> 1) If there implementation of otherFun method is correct, and when
> it's correct how can i call this method in main() function?


You can't.

> 2) why it doesn't work? (for simply function - not class member - it
> works fine)


Sure. There is a difference between a function and a member function
that is not static. You need to educate yourself on member funcitons
and "pointers to members".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      07-30-2007
"Pawel_Iks" wrote:

>I have following class definition (A.h file):
>
> class A
> {
> public:
> int N;
> int count1(int n) {n++;}
> int count2(int n) {n+=5;}
> int otherFun(int n, int (*fun)(int));
> }
>
> and I want to implement otherFun method (in A.cpp file):
>
> int A:therFun(int,int (*fun)(int))
> {
> int c=0,d;
> d=fun(c);
> }
>
> and call it (in main.cpp):
>
> int main()
> {
> A obj=A();
> int t;
> A.N=12;
> t=A.otherFun(N,test.count1);
> return 0;
> }
>
> and it doesn't work ... I have two questions:
> 1) If there implementation of otherFun method is correct, and when
> it's correct how can i call this method in main() function?
> 2) why it doesn't work? (for simply function - not class member - it
> works fine)


Try clicking on item 33 in this link.

http://www.parashift.com/c++-faq-lit...le-of-contents


 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      07-31-2007
On Jul 30, 3:39 pm, Pawel_Iks <pawel.labed...@gmail.com> wrote:
> I have following class definition (A.h file):
>
> class A
> {
> public:
> int N;
> int count1(int n) {n++;}
> int count2(int n) {n+=5;}
> int otherFun(int n, int (*fun)(int));
>
> }
>
> and I want to implement otherFun method (in A.cpp file):
>
> int A:therFun(int,int (*fun)(int))
> {
> int c=0,d;
> d=fun(c);
>
> }
>
> and call it (in main.cpp):
>
> int main()
> {
> A obj=A();

this works but it is better to simply write:
A obj;
> int t;
> A.N=12;
> t=A.otherFun(N,test.count1);


this syntax belongs to C# not C++. instance member functions are
different from static functions the 'A:therFunc' takes a static
function as its parameter.

you can overload 'A:therFunc' with this one:

class A{
public:
int otherFunc (int,A& ,int(A::*)(int) );
//the rest of class follows as before:
...
};

int A:therFunc (int n,A& obj_ref,int(A::*method)(int) ){
return (obj_ref.*method)(n);//call with object or referenc
};

int F(int){};
static int G(int){};

int main(){
A a,test;
a.otherFunc (1,test,&A::count1);//my version of otherFun
a.otherFunc (2,F);//your version of otherFun
a.otherFunc (3,G);//your version of otherFun
return 0;
};

> 2) why it doesn't work? (for simply function - not class member - it
> works fine)


because you think that combination of a method with an object forms a
static function but you are wrong.
It is obvious that you are migrating from a newly introduced high-
level language(java/C#) to the mixed-level C++.Things are different in
different languages.

regards,
FM.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
DocumentBuilder object is not able to parse a XML String which has a nodename which contains forward slash! Ed Java 6 08-02-2007 03:29 PM
Copy constructor for a class that contains a pointer to a base class type (newbie) SzH C++ 1 10-27-2006 09:21 PM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Regex problem, match if line contains <a>, unless it also contains <b> James Dyer Perl 5 02-20-2004 12:29 PM



Advertisments