Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
static methods access static member only, why?


Because, unlike ordinary methods, static ones haven't the hidden 'this' pointer as argument.
From another point of view, static methods are class ones, i.e. you don't need an instance of the class to call them: since the call is 'istanceless' you cannot access instance members.
(If you really need to, then you should explicitely pass an instance of the class).
Share this answer
Well, that's just common sense. Say you have three instances of Car. Each has a different color. Your static method can be called like this - Car.StaticMethod();

Now, as it's not called from one of your three instances, which of the three possibile colors for cars that exist in your system at that time should it return ? What if you have 500 cars ?
Share this answer
...because they (such methods)
do not have a this-context (they are quasi global)... :)

But the static functions may receive a context, for example:
/*static*/ DWORD WIANAPI CYourDlg::ThreadWorkerProc(CYourDlg* pcDlg)
  if (pcDlg) {
    while (pcDlg->m_bWorkerRunning /*access by received context*/) {
  return 0;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900