-3

I just to understand the logic behind this code. When member function is not part of the object then how does the compiler invokes the function. Somehow the compiler needs to know the address of the function to invoke. Where it gets the address of the right function.I know this is silly question but curious to understand the underlying truth behind this

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "Base class constructor\n";
    }
    void Fun()
    {
        cout << sizeof(this) << endl;
        cout << "This is member function" ;
    }

    void Fun1()
    {
        cout << "This is second member fun" << endl;
    }
    int Val;
};

int main(int argc, char* argv[])
{
   Base Obj;
   cout << sizeof(Obj) << endl;
   Obj.Fun();
    return 0;

}
  • 4
    I don't understand 1) your question; what `member function is not part of the object` means? 2) what your code demonstrates or what exactly you don't understand in it. –  Jun 27 '19 at 23:37
  • When we calculate the size of the object, you won't see member function occupying space as part of the part. – user3225861 Jun 27 '19 at 23:40
  • Uh? Member functions do not occupy any space (well, `virtual` functions do that most of the times, but it doesn't seem related to your question). – Yksisarvinen Jun 27 '19 at 23:43
  • I'm not sure how it's implemented in C++, but I suspect that function like `BaseFun(int Val)` was generated (i.e. `Base` class completely dies after compilation, and there are "static" functions which operates only with its `Val`). –  Jun 27 '19 at 23:45
  • *Somehow the compiler needs to know the address of the function to invoke.* No it doesn't. That is often the job of the linker. That said, sometimes the compiler throws out the function entirely and builds it right into the caller. – user4581301 Jun 27 '19 at 23:47
  • [How does the compilation/linking process work?](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) may help you sort out who is doing what and when. – user4581301 Jun 27 '19 at 23:49
  • This may be not what the question is about, but, what are those `sizeof`s meant to show? `sizeof(Obj)` is equivalent to `sizeof (Base)`, while `sizeof (this)` is equivalent to `sizeof (Base*)`. Which means: those aren't the same thing, and I am unsure what you wanted to show by that. – Algirdas Preidžius Jun 28 '19 at 00:04
  • A non-virtual function is not determined based on the object. It works exactly the same as if it were a non-member function `void Base_Fun(Base* self);` and the call were `Base_Fun(&Obj);`. – molbdnilo Jun 28 '19 at 07:07

1 Answers1

1

Somehow the compiler needs to know the address of the function to invoke.

The compilation toolchain is responsible for choosing where the functions will be located (let us ignore dynamic linking and address space randomisation for simplicity). That is why the toolchain knows where the function will be.

For example, let's say the toolchain chooses to locate the function Base::Fun in address 0x008. So, when the function Base::Fun is called, the toolchain knows that the call must be made into the address 0x008.

eerorika
  • 232,697
  • 12
  • 197
  • 326