How a C ++ object stores information about its member functions

class A {
       public :
       void printSometext() {
       std::cout << "printing A" << std::endl;
       }
    };
class B {
       public : 
       void printSometext() {
       std::cout << "printing B" << std::endl;
       }
    };

int main() {
   A* a = new A();
   a->printSometext();
   return 0;
}

As an object, C ++ stores information about its member functions. Let's look at the code above. When I call printSometext on the object "a", how does it know which function to call and how it finds the correct method. When printing the size of an object, it prints the summing size of its member variable (+ bindings). Therefore, please provide some internal information about how a member function is called.

Thanks Deimus

+3
source share
4 answers

Well, this is an interesting question, but let me try to answer it very methodically!

let the compiler have to resolve the call as follows: *

a-> SomeFunc ();

*.

.

1.) -, a, , a (lets call this, class A for time being) someFunc() . class A, A, , public.

  • , .

2.) -, A, , ( ). overloading resolution. , , . , someFunc() s someFunc() ( ).

3.) . , someFunc(), , A (lets call this class AA and needless to say it is some subclass of A), a ( ​​ A) AA ( ++ ). , someFunc() virtual, (.. A) someFunc() (A) A ( AA, A AA), someFunc() .

  • , , , AA . , AA , A, A A 1 !!!, , , someFunc() AA ( A AA), . , ( , ) , ( ) someFunc(), A AA. , . , .

  • : " ", ?... . , , , , Virtual Table . , ( ) . . A AA . someFunc() AA ( , a AA), AA. .

Avid

+4

++ . a printSomeText , , , . .

+7

(+1):

, , . , .

+3

. , → , . , , A:: printSomeText.

-

+1

All Articles