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

This pointer is available to each member function as a hidden implicit argument. It is the object address value as a pointer. 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)
  {
  }
10  };
11  C cpp_obj;
12  void main(int argc, char *argv[])
13  {
14    C::static_function();/*Object not needed*/
15    cpp_obj.static_function();
16    cpp_obj.nonstatic_function();
17  };

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)
  {
10      cout << "This pointer is " << This << endl;
11    }
12 
13  };
14  C cpp_obj;
15  int main(int argc, char* argv[])
16  {
17    /* Call with scope resolution and pass obj address*/
18    C::static_function(&cpp_obj);
19 
20    /* Call with object and pass obj address*/
21    cpp_obj.static_function(&cpp_obj);
22    return 0;
23  }

output

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

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

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14