#Previous question #Index of C++ Questions #Next question

This pointer and member functions

This pointer is available to each member function as a hidden implicit argument. It is the object address value as a pointer. Compiler internally passes this pointer to the function as the first argument.

class C
{
  public:
  void function1(int arg)
  {
  }
  void function2(int arg1, int arg2)
  {
  }
};
void main(int argc, char *argv[])
{
  C cpp_obj;
  cpp_obj.function1(1); 
  /* internally C::function1(&cpp_obj, 1); */
  
  cpp_obj.function2(1,2);
  /* internally C::function2(&cpp_obj, 1, 2); */
}

This pointer and static member functions

For static functions there is no object address at all because static function can be called even before creation of any object using scope resolution operator. Even calling with an object pointer will not associate any "this" pointer to it. Thus this pointer is not available in any static function.

Example:

class C
{
  public:
  static void static_function(void)
  {
  }
  void nonstatic_function(void)
  {
  }
};
C cpp_obj;
void main(int argc, char *argv[])
{
  C::static_function();/*Object not needed*/
  cpp_obj.static_function();
  cpp_obj.nonstatic_function();
}

Program disassembly VC++

14:       c.static_function();
00401058   call        @ILT+25(C::static_function) (0040101e)
15:       C::static_function();
0040105D   call        @ILT+25(C::static_function) (0040101e)
16:       c.nonstatic_function();
00401062   mov         ecx,offset cpp_obj (004235c4)
00401067   call        @ILT+30(C::nonstatic_function) (00401023)

Note:

  • 14: Directly called static_function even if object is there
  • 15: Directly called static_function
  • 16: The assembly instruction lea ecx,[ebp-4] loads address of c which is this to ecx. ecx is used to access the this inside member fuction

Passing this to static function

In the above discussion we have seen this pointer is not available in static function. Now there are situations where we require to access the object pointer or this inside static functions. However those situations can be handled by adding first argument as the pointer to the class to pass the address of the object.

Source code

#include <iostream>
using namespace std;
class C
{
  public:
  static void static_function(*This)
  {
    cout << "This pointer is " << This << endl;
  }
};
C cpp_obj;
int main(int argc, char* argv[])
{
  /* Call with scope resolution and pass obj address*/
  C::static_function(&cpp_obj);
  /* Call with object and pass obj address*/
  cpp_obj.static_function(&cpp_obj);
  return 0;
}

output

This pointer is 00477728
This pointer is 00477728
Press any key to continue

static main function in JAVA or C#

Java is fully object oriented language. Any function should be written inside a class. Now C/C++ written executables depends on a main function as an entry point. So to resolve this, java runtime finds the static member function named as main. Then it calls at the startup time. The following source code illustrates on how public static void main() of a Java class starts executing by Java runtime.

/* Understanding JAVA runtime calling main() */
#include <iostream>
using namespace std;
class Java
{
  public:
  static void main(char* argv[])
  {
    cout << "Hello from Java "  << endl;
  }
};
int main(int argc, char* argv[])
{
  Java::main(argv);
  return 0;
}
Hello from Java 

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students