Thread: Classes and static call back procedures

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Classes and static call back procedures

    I understood this a while back, but now, with more knowledge of Windows, I don't understand:

    When a callback procedure is a class member function, it must be declared static, so that there is a copy available at all times. But, in this code, there IS already a copy available.

    Code:
    class benny
    {
    public:
    LRESULT CALLBACK WndProc(params);
    BOOL Create(params);
    };
    
    LRESULT CALLBACK benny::WndProc(params)
    {
    blah
    }
    
    BOOL benny::Create(params)
    {
    WNDCLASSEX wcx;
    
    wcx.lpfnWndProc=WndProc;
    There IS already an instance of the window procedure, because for the Create function to run, someone must have created an instance of the class. Why then, can I not do away with static?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    No...the windows subsystems doesnt know or care about if there's an instance of a class.....

    static member functions exist before during & after an object is created, that's why they can be used as callbacks for windows

    Non-static members can only be called when an object has been created - they need an extra param so to speak by way of the "this" pointer, so due to this, they cannot be used as a callback for a windows proc

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Oh. What about if you create a class, then pass a pointer to a member function to another class. Will that work, if the member function is not static?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bennyandthejets
    Oh. What about if you create a class, then pass a pointer to a member function to another class. Will that work, if the member function is not static?
    If it's a nonstatic member function - no...because for the windows subsystem to call that function correctly, it would need to have a "this" pointer....

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'm not sure I understand about the this pointer. Is it passed implicitly to every member function? Could you show an example?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bennyandthejets
    I'm not sure I understand about the this pointer. Is it passed implicitly to every member function? Could you show an example?
    That's right...it's passed implicitly....it represents the identity of a specific object loaded into memory....but I cant show an example really as it's all controlled by the compiler......but just imagine it as an extra param that identifies a specific object

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So in the following code, it is the compiler that prevents me from doing it?

    Code:
    class benny
    {
    public:
    void DoStuff();
    (* void()) RetrieveFunc();
    };
    
    void benny::DoStuff()
    {
    }
    
    (* void()) Benny::RetrieveFunc()
    {
    return DoStuff();
    }
    Note: I know the code is probably not done right, but you probably know what I mean.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    about the this pointer, whenever you're in a member function, and you're directly accessing its members, you are using the this pointer, just a shorthand version of it.
    Code:
    class Foo
    {
    public: 
         void Bar(int a)     { b = a; }   // 'b' is using this
         void Boo(int a)     { this->b = a; } // same thing as above
    
    private:
         int b;
    };
    At least I'm pretty sure that's the idea.

    This is an implicit pointer to the object making the function call.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Don't even remember this thread, it was a whole month ago. I understand now why I can't do what I wanted, because of calling conventions. I'm doing some assembly language programming now to get a better idea of what happens under the hood, that should clear everything up completely.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    It is possible to explicitly specify a this pointer for a non static member function without using any assembler code, if thats what you are looking for.

    There is a post about it here.
    [code]

    your code here....

    [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. sharing a static varable across classes
    By steve1_rm in forum C++ Programming
    Replies: 2
    Last Post: 01-31-2009, 01:17 AM
  3. C++.NET, static classes
    By Magos in forum C++ Programming
    Replies: 1
    Last Post: 08-13-2008, 05:08 PM
  4. call methods across classes
    By vb.bajpai in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 01:31 PM
  5. static classes vs objects
    By earnshaw in forum C# Programming
    Replies: 5
    Last Post: 02-08-2006, 03:19 PM