Home > Net >  Objects classes and Member Function
Objects classes and Member Function

Time:08-30

Executed with Online GDB Compiler.

#include <iostream>

using namespace std;

class test {
public:
  int x = 5;
  double z = 5.89999;
  float y = 6.7;

  void print(){
    int m;
    cout << "Print Method" << endl;
  }
};

int main(){
  test t;

  cout << *(int*)((char*)&t   offsetof(test, x)) << endl;
  cout << *(float*)((char*)&t   offsetof(test, y)) << endl;
  cout << *(double*)((char*)&t   offsetof(test, z)) << endl;

  //Accessing the pointer to the member function

  void (test::*mf)() = &test::print;

  //Can we do the above statement using pointer arithmetic

  //Making a call to it

  (t.*mf)();

  //Can we do the above statement using pointer arithmetic?
}

Output:

5
6.7
5.89999
Print Method

I might have a lot of mistakes in my understanding, so kindly excuse me.

A function name points to the starting address of the code segment of its own function.

  1. I created a pointer to the member function void (test::*mf)() and it is assigned with the pointer of the member function print. How can I access the member function's address using the class name? Where does the class name reside in memory, and how is it associated to the member function with respect to the address and memory space?

  2. Memory space gets allocated for the member fields once the object is created, and objects don't have the pointer to the code segment of the member functions. So, a pointer to a member function is not part of the object, but can be invoked by the object. How does the object get a hold of that member function's pointer, and where is it hiding?

  3. void (test::*mf)() = &test::print;

    Can I do the above statement using pointer arithmetic?

    (t.*mf)();

    Can I do the above statement using pointer arithmetic?

CodePudding user response:

How can I access the member function's address using the class name?

You don't.

Where does the class name reside in memory, and how is it associated to the member function with respect to the address and memory space?

The class name is not stored anywhere (well, except maybe in RTTI memory, but we'll ignore that since it is not relevant to this discussion).

The compiler knows that the member function is associated with the class, so it can perform access checks and such. And the compiler knows what kind of object the member function is being called on, so it can pass the object in the function's hidden this parameter. But the actual code segment of the function is stored elsewhere in memory, and the compiler doesn't knows where that actually is. All it can do is make reference to it, and then the linker will fill in that reference with the appropriate memory access when creating the executable file.

Memory space gets allocated for the member fields once the object is created, and objects don't have the pointer to the code segment of the member functions. So, a pointer to a member function is not part of the object, but can be invoked by the object. How does the object get a hold of that member function's pointer

It doesn't. The object doesn't invoke anything. The compiler does, it generates a normal function call like any other, but it passes the address of the associated object as the function's hidden this parameter.

and where is it hiding?

It is not hiding anywhere.

Can I do the above statement using pointer arithmetic?

No, to both, because the code segment of a member function is not part of the object's allocated memory, like data members are, so you can't reach a member function by calculating an address relative to the object itself.

CodePudding user response:

why don't you use pointers with test t; test * p_t = &t; p_t ->print(); I think this is a better choice when you have just created a great pointer that has one object to use concurrently

  • Related