473,769 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using method function pointers in C++

Hello friends,

I am implementing something like

PM
|------------------------------------------------------
| | |
| | ------- -| send_if( data) |
| | M | |
| | | |
| | -------- | | |
| | | |
-----------------------------------------|-------------
|
PS |
|--------------------------------------------------------------
| | |
| | ------- | receive_if( data) |
| | S | |
| | | |
| | --------| |
| | |
------------------- --------------------------------------------

In this implementation, control flow starts with M's executeM() method.
executeM() is supposed to pass data to interface, say send_if( char *).
send_if( char *data); passes on this data to SInterface
send_if( char *data)
{
receive_if( data);
}

receive_if( ) collects this data and passes on to S by

receive_if( char * data)
{
executeS( data);
}
this will transfer data to S and S can display or do whatever it wants.
*************** *****

The class structure which i have created for this is :

1. File named: M_if.h
class M_if
{
public:
void send_if( char *);

};

*****
2. File: M.h
class M
{
char *data;
public:
M( )
{
data = new char[5];
data = "C++";
}

void executeM()
{
funcPtr_sendIf( data);
}

/// A fucntion pointer to store send_if() address.
void ( *funcPtr_sendIf ) ( char *);

};
class PM: public M_if
{
public:
M *m;

PM() // constructor
{
m = new M();
m-> funcPtr_sendIf = &send_if;
}

//// Send interface present with PM
void send_if()
{
funcPtr_receive If( data);
}

void ( *funcPtr_receiv eIf) ( char *);
};

******
3. File named: S_if.h

class S_if
{
public:
void receive_if( char *);

};

*****
2. File: S.h
class S
{
char *data;
public:
S() { } /// constructor

//// S Execution function, to display data
void executeS( char * data)
{
cout << " Data is : " << data;
}

};
class PS: public S_if
{
public:
S *s;

PS()
{
s = new S();
}

void receive_if( char * data)
{
s->executeS( data);
}
};

******

4. TopM

class TopM
{
PM *pm;
PS *ps;

public:
TopM ()
{

pm = new PM();
ps = new PS();

//// Interconnecting by assigning function pointer
pm->funcPtr_receiv eIf = &(ps->receive_if);
}
};

*************** *************** *************** ***

Of course, it doesn't get compiled.

I went through function pointer implementations and whatever i could
understand was what i have done in this example.

Please guide in this journey through method function pointer in C++

Jun 9 '06 #1
1 2882
You can not define the function pointer of one class as so.
/// A fucntion pointer to store send_if() address.
void ( *funcPtr_sendIf ) ( char *);

If I didn't remember wrong, it should be
void(M_inf::*fu ncPtr_sendif)(c har*).

Very strange? Yes, C++ has more strange syntax.

Regards.

Kandy
Pankaj wrote:
Hello friends,

I am implementing something like

PM
|------------------------------------------------------
| | |
| | ------- -| send_if( data) |
| | M | |
| | | |
| | -------- | | |
| | | |
-----------------------------------------|-------------
|
PS |
|--------------------------------------------------------------
| | |
| | ------- | receive_if( data) |
| | S | |
| | | |
| | --------| |
| | |
------------------- --------------------------------------------

In this implementation, control flow starts with M's executeM() method.
executeM() is supposed to pass data to interface, say send_if( char *).
send_if( char *data); passes on this data to SInterface
send_if( char *data)
{
receive_if( data);
}

receive_if( ) collects this data and passes on to S by

receive_if( char * data)
{
executeS( data);
}
this will transfer data to S and S can display or do whatever it wants.
*************** *****

The class structure which i have created for this is :

1. File named: M_if.h
class M_if
{
public:
void send_if( char *);

};

*****
2. File: M.h
class M
{
char *data;
public:
M( )
{
data = new char[5];
data = "C++";
}

void executeM()
{
funcPtr_sendIf( data);
}

/// A fucntion pointer to store send_if() address.
void ( *funcPtr_sendIf ) ( char *);

};
class PM: public M_if
{
public:
M *m;

PM() // constructor
{
m = new M();
m-> funcPtr_sendIf = &send_if;
}

//// Send interface present with PM
void send_if()
{
funcPtr_receive If( data);
}

void ( *funcPtr_receiv eIf) ( char *);
};

******
3. File named: S_if.h

class S_if
{
public:
void receive_if( char *);

};

*****
2. File: S.h
class S
{
char *data;
public:
S() { } /// constructor

//// S Execution function, to display data
void executeS( char * data)
{
cout << " Data is : " << data;
}

};
class PS: public S_if
{
public:
S *s;

PS()
{
s = new S();
}

void receive_if( char * data)
{
s->executeS( data);
}
};

******

4. TopM

class TopM
{
PM *pm;
PS *ps;

public:
TopM ()
{

pm = new PM();
ps = new PS();

//// Interconnecting by assigning function pointer
pm->funcPtr_receiv eIf = &(ps->receive_if);
}
};

*************** *************** *************** ***

Of course, it doesn't get compiled.

I went through function pointer implementations and whatever i could
understand was what i have done in this example.

Please guide in this journey through method function pointer in C++


Jun 9 '06 #2

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

Similar topics

6
95885
by: S Manohar | last post by:
I need to pass a 'reference' to a double value to a function which changes that value. Each time the function is called, it needs to change different sets of values. In C I'd simply pass references. void add(double *a1, double *a2, double *a3){ *a1 = *a2 + *a3; *a2 = *a1 + *a3; } In Java, one solution would be to create a class to represent a
10
6460
by: emerth | last post by:
I am curious about the structure of a C++ object. Classes have methods, and data. Different instances of a class will have their own data variables (yes, unless I define a variable to be common, but let us say I did not do that). My question is this: does each instance have it's own copy of the non-static methods?
8
2068
by: Ed Fair | last post by:
Hi, I'm a long-time C programmer, I've started making the transition to "real C++" recently. I am puzzled about how to write a factory method. Any suggestions, pointers or references to documentation would be appreciated. Here is some background information about what I'm doing: I have written a large body of code that makes extensive use of mutexes. Most of my code is portable, except for the mutexes, which are platform specific. ...
12
1933
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the board. Since this function does not change anything in the class I declared it as const. To count all pieces of a given color the functions iterates through a "map" of CNode-pointers. "CNode" is another class that is irrelevant to the problem.
1
2352
by: dennis luehring | last post by:
i try to "publish" some of my very stupid methods (i need this for an interpreter) function_one procedure_one here is the code --------- class core {
19
676
by: Kamilche | last post by:
I have looked at many object-oriented programming frameworks out there for C. Though the ideas presented are intriguing, and I've used some of them in my own work, they all suffered some drawback or another. I have created a new method of doing OOP with C, something that fits my needs particularly well. I haven't encountered anything else like it, so I'm tossing it out to the Internet for comment. ...
12
2110
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
3
1990
by: Rennie deGraaf | last post by:
Let's say that I want to read a method pointer in from a stream. (I'm not saying that this is a good design idea, or that I actually have a reason to do this.) If I wanted to read in a function pointer, I could do something like this: #include <iostream> int*(*readFunction())(int, int*) { unsigned long x;
2
3059
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is what I'm hoping to achieve. I've never before had to use Javascript closures, but now I do, so I'm making an effort to understand them. I've been giving this essay a re-read: http://jibbering.com/faq/faq_notes/closures.html
3
2224
by: Schizoid Man | last post by:
Hi, I'm a novice whose just about migrating to C++ after cutting my teeth on C for a few years. To start with I'm using Microsoft's out-of-the-box Visual C++ Express Edition compiler and I had a couple of questions: 1. If I am using namespace std, is there a specific reason for me to place std:: before all the manipulators - cout, endl, etc? I've noticed
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10047
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
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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
7410
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
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
3
2815
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.