Please explain, if I have a virtual method in the base class and I redefine it in the derivative, should the addresses of these methods coincide? I have a base class Book. It contains the virtual function Show ():

virtual void Show() { showISBN(); showAuthor(); showTitle(); } 

There is a derived class CardFile, in which I override Show ():

 void Show() override { Book::Show(); cout << "Number of refuge: " << refugeNumber << endl; cout << "Stock: " << stock << endl; } 

Next, in the console, I display the addresses of the methods in this way:

 printf("Book::Show -- %p\n", &Book::Show); printf("CardFile::Show -- %p\n", &CardFile::Show); 

But for some reason I get different values. So it should be?

  • one
    So, where did the question come from? At first glance (even if you ignore what is written in my answer) you type the addresses of two different functions. Why are you surprised that these addresses are different ??? - AnT
  • Once there was a generally incomprehensible case when the addresses matched. This really surprised me ... - Andrey_Raf
  • @Andrey_Raf Well, if you display them in this way, they can easily match. The fact is that a pointer to a class method is not a regular pointer and cannot be printed through %p . This is usually a 3 times larger structure than a normal pointer. So its contents should be printed byte- sizeof(&Book::Show) bytes. - VTT
  • @Andrey_Raf: "The addresses matched" - this is exactly what I write about in my reply. But in general, nothing can be said about these addresses. - AnT
  • Thank you very much for the clarification - Andrey_Raf

1 answer 1

(Well, first, typing function pointers through %p not formally allowed.)

No, they should not. There really should not be anything concrete or predictable. The values ​​of the pointer to a virtual function are unspecified and the results of comparing such a pointer to equality with pointers to other functions are unspecified .

7.6.10 Equality operators
4 [...] Comparing pointers to members is defined as follows:
- If either is a virtual member function, the result is unspecified.

The real reasons for this situation lie in the peculiarities of the behavior of pointers to virtual functions in C ++: they never bind to specific objective functions. The decision about which virtual function will be called is made at the time of the call, and not at the time of pointer initialization.

In order to implement this behavior in practice, in traditional implementations a pointer to a virtual function usually indicates not the function itself, but a hidden intermediate "caller function". The same "caller function" can be used for different virtual functions, with the result that pointers to different virtual functions can have the same physical meaning. And vice versa, for different classes different “function-callers” can be used, which will lead to the difference of pointers, even if they eventually call the same virtual function.

The last detail as once explains the behavior you observe.