Re: Static member functions in VC

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Gnum (no-email_at_mail.boo)
Date: 05/11/04

  • Next message: sklett: "Re: determining what a ptr is pointing to"
    Date: Wed, 12 May 2004 09:20:01 +1200
    
    

    You're right of course.
    What I want to achieve is NON static member function to be assigned as
    Callback procedure.
    It is exactly right that it HAS TO contain pointer to an object itself.
    One again you're right it is NOT standard C++ pointer as it is implemented
    in Borland C++ Builder by special way as Igor Tandetnik suggested here.
    And you're right it is like .Net delegate.

    My question was HOW TO say VC++ to handle it with no requiring me to write
    fancy macros and handle it manually.
    I do know how to do it, but I don't want to waste my time before I ensure
    there is no other way around.

    By the way. Thank you all guys! Now i know that I HAVE TO handle it bymeself
    as Microsoft hasn't implemented any common solution of this problem in VC++
    6.0 OR it is hidden so far that I will never know about it.

    Cheers!
    Gnum

    "Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
    news:0jb2a0tkvtb3u4uucv5ndn5n37apq23d31@4ax.com...
    > Gnum wrote:
    >
    > >I've got a problem:
    > >
    > >====================================================
    > >typedef void FOnEven( int );
    > >
    > >class clsA
    > >{
    > >private:
    > >public:
    > > FOnEven* OnEvent;
    > >};
    > >
    > >class clsB
    > >{
    > >private:
    > > clsA* A;
    > >public:
    > > clsB();
    > > static void Event( void );
    > >}
    > >
    > >clsB::clsB()
    > >{
    > > A = new clsA();
    > > A->OnEvent = Event;
    > >}
    > >
    > >void clsB::Event( void )
    > >{
    > > From inside this function I can't see Data members of the object as
    > ><this> point is not defined here.
    > > However in Borland C++ Builder this construction allows me to see
    them.
    >
    > That can't be. The "this" pointer is not available inside static member
    > functions like "Event". Period. Mario gave you one possible workaround.
    >
    > > I can't realize how to implement the same ability in MS Visual C++.
    > > I don't want to write long tricky and looking weird things to get
    around
    > >this restriction to assign
    > > only STATIC functions pointers to variables like my FOnEven* OnEvent;
    > >
    > > Does anyone know some general solution?
    > > I wish there was a tickbox in settings to say "Allow assign NOT
    STATIC
    > >member functions to CALLBACKs"
    > >}
    >
    > Are you sure you're not confusing normal function pointers with pointers
    to
    > (non-static) member functions? Normal C++ pointers to members don't
    contain
    > an instance pointer. However, you can create a class that encapsulates an
    > instance pointer and a pointer to member function, which would then be
    much
    > like a .NET delegate. Maybe C++ Builder provides something like that? If
    so,
    > then your "Event" function would be a non-static member function.
    >
    > --
    > Doug Harrison
    > Microsoft MVP - Visual C++


  • Next message: sklett: "Re: determining what a ptr is pointing to"

    Relevant Pages

    • Re: Function Pointer Query
      ... Note that pointer to member function is incompatible with regular ... needs a callback, you are sadly out of luck. ... void EnumWindows; ...
      (microsoft.public.vc.language)
    • Re: Why pointer to member function?
      ... I want a simple,clean,cheap way that can save the member function ... fixed for any type of closure. ... don't want to save a pointer to a pointer to a member function ... the boost lib does prove that I'm not wrong on this. ...
      (comp.lang.cpp)
    • Re: c[++] pointer question
      ... A pointer is a variable that contains a memory address. ... The above are member variables. ... A constructor is a special member function that is meant to initialize ... In modern C++ the initialization is better done via an initializer list, ...
      (comp.lang.cpp)
    • Re: A C++ Whishlist
      ... > You invoke undefined behaviour and the compiler is free to do as it ... > calls a member function via a NULL pointer. ... > means that it doesn't invoke undefined behaviour, ... > undefined behaviour until you invoke a member function via a NULL ...
      (comp.lang.cpp)
    • Re: Basic question regarding checkboxes
      ... It appears that your real problem is not understanding that the above call is to a C++ member function. ... Your error message shows you are not calling the MFC version of CheckDlgButton but attempting to call the API version. ... The MFC version is a member function of the dialog and it "knows" the HWND of the dialog. ... If you are making this call in some code module that is not part of the dialog then first you have to call that module, passing it the 'this' pointer from the dialog object. ...
      (microsoft.public.vc.mfc)