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.
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.
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)
- 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.
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.
Hello from Java
About our authors: Team EQA
You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.
Learn on Youtube