why a static function only accept external static variables?

Aug 3, 2015 at 12:35pm
why a static function only accept external static variables and not no-static?
Aug 3, 2015 at 1:01pm
What do you mean by "no-static"?

You have a class Dog with static member function Chase(). The class has member var Tail.

You want to touch a Tail within Chase?

I have this code:
1
2
3
Dog kennel[101];

Dog::Chase();

The tail of which resident of the kennel are we supposedly chasing?
Aug 3, 2015 at 1:03pm
Static function only access static variable in c++ because instance variable has to be accessed by objects and static function are accessed by class name and its execution during runtime is much more faster than class member function.
Aug 3, 2015 at 3:10pm
but a LRESULT CALLBACK function must be static inside of a class :(
Aug 3, 2015 at 6:29pm
Callback functions typically allow you to pass a pointer (or something that can be used as a pointer) to the callback functions. Use it for the class's address so you can invoke it directly in the callback.

Here's a fun example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <vector>

#ifndef NOMINMAX
#define NOMINMAX
#endif
extern "C" 
{
  #include <windows.h>
}

//----------------------------------------------------------------------------
// EnumWindows() -- return a list of toplevel window handles
class EnumerateWindows: public std::vector <HWND>
{
private:
  // Here's the class's callback method ......................................
  BOOL EnumWindowsMethod( HWND hWnd )
  {
    push_back( hWnd );
    return TRUE;
  }

  // Here's the static callback procedure ....................................
  static BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam )
  {
    return reinterpret_cast <EnumerateWindows*> (lParam)->EnumWindowsMethod( hWnd );
  }
  
public:
  // Here's the function that makes it all happen ............................
  EnumerateWindows()
  {
    EnumWindows( EnumWindowsProc, (LPARAM)this );
  }
};

//----------------------------------------------------------------------------
// GetWindowText() -- return a window's titlebar text
std::string GetWindowText( HWND hWnd )
{
  std::string s( MAX_PATH, '\0' );
  s.resize( GetWindowText( hWnd, const_cast <char*> (s.c_str()), MAX_PATH ) );
  return s;
}

//----------------------------------------------------------------------------
int main()
{
  std::cout << "Toplevel windows (that actually have titles) are:\n";
  for (const auto& handle : EnumerateWindows())
  {
    const std::string& title = GetWindowText( handle );
    if (!title.empty())
      std::cout << "  \"" << title << "\"\n";
  }
}

And yes, I did just inherit from std::vector. Nyaah!
Some purists' feelings may have been harmed during the making of this commercial.

Enjoy!
Aug 3, 2015 at 7:13pm
thanks Duoas.
let me ask: you used the lParam for HWND. but imagine that if you need, too, the wParam?
LRESULT CALLBACK MsgBoxHook(int nCode, WPARAM wParam, LPARAM lParam)
like you see my CALLBACK have more parameters. can you explain more, please?
Aug 3, 2015 at 11:52pm
Marty McFly in 'Back to the Future' said:
Damn it, Doc! Why did you have to tear up that letter? If only I had more time... Wait a minute, I got all the time I want! I got a time machine!

Um, you can make it look however you like.

1
2
3
4
5
6
7
8
9
10
  LRESULT MsgBoxHook( int nCode, WPARAM wParam )
  {
    std::cout << "Yeah!\n";
    return 0;
  }

  static LRESULT CALLBACK MsgBoxHook( int nCode, WPARAM wParam, LPARAM lParam )
  {
    return reinterpret_cast <Fooey*> (lParam)->MsgBoxHook( nCode, WParam );
  }

Hope this helps.

Sorry, I couldn't find an online video with the clip above in it.
Aug 4, 2015 at 12:00am
thanks Duoas. thanks for all.
now i can use a static CALLBACK, only, for call the non CALLBACK function, that i don't need a static members for use them. thanks for alll. really thanks
Aug 4, 2015 at 10:17am
on:
1
2
3
4
5
LRESULT MsgBoxHook( int nCode, WPARAM wParam )
  {
    std::cout << "Yeah!\n";
    return 0;
  }

i must 'calculate' the pointer or i will get bad results, wright?
Aug 4, 2015 at 4:26pm
I don't understand your question.
Aug 4, 2015 at 4:33pm
i had tryeid the code sample on my code. but i was getting unexpeted results :(
but i will trying again
Aug 4, 2015 at 4:58pm
You need:

1) a static function which matches the expected callback, and calls the member function
2) a member function, which should look just like the static function except without the lParam argument
3) a function that initializes the callback mechanism, passing the object's instance as the lParam 'data' argument.
Aug 4, 2015 at 5:07pm
maybe that's why :P
i was using the lParam
Topic archived. No new replies allowed.