why a static function only accept external static variables?

why a static function only accept external static variables and not no-static?
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?
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.
but a LRESULT CALLBACK function must be static inside of a class :(
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!
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?
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.
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
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?
I don't understand your question.
i had tryeid the code sample on my code. but i was getting unexpeted results :(
but i will trying again
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.
maybe that's why :P
i was using the lParam
Topic archived. No new replies allowed.