Click here to Skip to main content
15,880,427 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to declare pointer to member function Pin
Danzy8327-Aug-10 14:02
Danzy8327-Aug-10 14:02 
AnswerRe: How to declare pointer to member function Pin
Luc Pattyn27-Aug-10 15:08
sitebuilderLuc Pattyn27-Aug-10 15:08 
AnswerRe: How to declare pointer to member function Pin
wsauer27-Aug-10 18:12
wsauer27-Aug-10 18:12 
GeneralRe: How to declare pointer to member function Pin
Aescleal27-Aug-10 20:19
Aescleal27-Aug-10 20:19 
GeneralRe: How to declare pointer to member function Pin
Niklas L27-Aug-10 21:15
Niklas L27-Aug-10 21:15 
GeneralRe: How to declare pointer to member function Pin
Aescleal27-Aug-10 22:21
Aescleal27-Aug-10 22:21 
AnswerRe: How to declare pointer to member function Pin
«_Superman_»27-Aug-10 18:20
professional«_Superman_»27-Aug-10 18:20 
AnswerRe: How to declare pointer to member function Pin
Paul Michalik28-Aug-10 0:55
Paul Michalik28-Aug-10 0:55 
The c++ syntax and semantics for pointers to members is admittedly confusing. You have seen the basic rules above. I cannot agree however with some of the statements posted above, especially this one:

Use with care, because they (usually) mean that there is something about your design that you haven't thought through properly. I can't think of any situation right now where you can't use polymorphism to solve the same problem in code that you own. I haven't encountered any member function pointers in my job yet


Consider the inversion of control idiom which is inevitable if designing loosely coupled component systems. Usually, you will want to implement something out of the publisher/subscriber family of patterns, like some sort of event/delegate or signal and slot mechanism. In c++, pointers to class member functions are a very handy way to do that. Fortunately, you do not have to use the "raw" c++ syntax - wise men have been working on that and gave us things like std::tr1::function (skip tr1 for c++0x), or even boost::signal and boost::signal2. Here "delegates" to c++ class member functions are quite easy to use:

// in context 1
namespace SubscriberContext {

 class Subscriber {
  int mID;

  public:

   bool FileMenu(void) {
    std::out << "FileMenu of object " << mID << " called" << atd::endl;
    return true;
   }

  public:

   Subscriber(int pID) : mID(pID) {}
 };

}

// in some other context where "Subscriber" is not known

#include <functional>
#include <vector>

namespace PublisherContext {

 class Publisher {
  public:
   
   typedef std::tr1::function<bool ()> Delegate;

  private:

   std::vector<Delegate> mSubscribers;

   static void Invoke(Delegate pD) { pD(); }

  public:

   void Connect(Delegate pD) {
    mSubsribers.push_back(pD);
   }
 
   void Disconnect() { 
    mSubscribers.clear();
   }

   void Publish() {
    std::for_each(mSubscribers.begin(), mSubscribers.end(), Publisher::Invoke);
   }
 };

}

// wiring'em up:

int main() {
 using namespace std::tr1;
 using namespace PublisherContext;
 using namespace SubscriberContext;

 Publisher tPub;
 Subscriber tSubs1(1), tSubs2(2), tSubs3(3);

 // connect
 tPub.Connect(bind(mem_fn(&Subscriber::FileMenu), tSubs1));
 tPub.Connect(bind(mem_fn(&Subscriber::FileMenu), tSubs2));
 tPub.Connect(bind(mem_fn(&Subscriber::FileMenu), tSubs3));

 // do work
 tPub.Publish();

 // disconnect
 tPub.Disconnect();
}


Huh, I just wrote it down without trying to compile, so please, be tolerant... There is more to that than this, therefore I would recommend to have a look at boost::signals. With "naked" std::tr1::functions, like in the example above, you cannot, for example, disconnect one specific stub, since td::tr1::function objects are not comparable. But I hope you get the point.

Cheers,

Paul
GeneralRe: How to declare pointer to member function Pin
Aescleal28-Aug-10 4:52
Aescleal28-Aug-10 4:52 
GeneralRe: How to declare pointer to member function Pin
Paul Michalik28-Aug-10 9:52
Paul Michalik28-Aug-10 9:52 
GeneralRe: How to declare pointer to member function Pin
Aescleal28-Aug-10 11:10
Aescleal28-Aug-10 11:10 
GeneralRe: How to declare pointer to member function Pin
Paul Michalik28-Aug-10 21:40
Paul Michalik28-Aug-10 21:40 
QuestionAfxGetMainWnd() returns the window of another application!!!! Pin
VCSharp00727-Aug-10 0:08
VCSharp00727-Aug-10 0:08 
Questionwindow controls are not getting resized while dragging the window Pin
arun_pk27-Aug-10 0:04
arun_pk27-Aug-10 0:04 
AnswerRe: window controls are not getting resized while dragging the window Pin
Cool_Dev27-Aug-10 2:24
Cool_Dev27-Aug-10 2:24 
AnswerRe: window controls are not getting resized while dragging the window Pin
David Crow27-Aug-10 6:46
David Crow27-Aug-10 6:46 
QuestionProblem in binary file writing Pin
ganesh_IT26-Aug-10 23:52
ganesh_IT26-Aug-10 23:52 
AnswerRe: Problem in binary file writing Pin
Sauro Viti27-Aug-10 0:01
professionalSauro Viti27-Aug-10 0:01 
QuestionBinary file read writing problem Pin
ganesh_IT26-Aug-10 23:46
ganesh_IT26-Aug-10 23:46 
AnswerRe: Binary file read writing problem PinPopular
CPallini26-Aug-10 23:51
mveCPallini26-Aug-10 23:51 
AnswerRe: Binary file read writing problem Pin
ThatsAlok29-Aug-10 20:59
ThatsAlok29-Aug-10 20:59 
QuestionGUI Pin
T.RATHA KRISHNAN26-Aug-10 20:10
T.RATHA KRISHNAN26-Aug-10 20:10 
AnswerRe: GUI Pin
Niklas L26-Aug-10 22:00
Niklas L26-Aug-10 22:00 
GeneralRe: GUI Pin
T.RATHA KRISHNAN26-Aug-10 23:05
T.RATHA KRISHNAN26-Aug-10 23:05 
GeneralRe: GUI Pin
Niklas L26-Aug-10 23:22
Niklas L26-Aug-10 23:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.