473,725 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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::otherFun(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)

Jul 30 '07 #1
3 2140
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::otherFun(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
Jul 30 '07 #2
"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::otherFun(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
Jul 30 '07 #3
On Jul 30, 3:39 pm, Pawel_Iks <pawel.labed... @gmail.comwrote :
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::otherFun(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::otherFunc' takes a static
function as its parameter.

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

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

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

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

int main(){
A a,test;
a.otherFunc (1,test,&A::cou nt1);//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.
Jul 31 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5122
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In half-code this should in the end look like this: In the child class:
77
4041
by: Eltee | last post by:
Hi everybody, Is it possible to 1. call a function from a dll made with .NET (C#) 2. from a program written in plain (as in: not .NET) C or C++? To be more specific, this is what I have. 1. C# file Class.cs:
3
11877
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a to-be-developed-function cmdOkay_Click() function as described below. 1. Create an user control that contains an OK button as below Public Class MyButton:System.Windows.Form.UserControl
24
7687
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
0
1651
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int myVal);
15
12788
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } class MyClass :ThirdPartyClass, IDisposable { void IDisposable.Dispose() {
2
1604
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in ASP.NET I would like to add this to the Session object whilst using SQLServer as the session storage. Unfortunately, as this required the RCW class to be serializable, this won't work, and throws an exception saying you can't Serialize the Session...
5
2317
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in commented code for anyone else who may be looking to do the same.
7
3303
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.