c++ - calling the correct virtual method using member function pointer -


In the example below, where I tried to minimize the problem, there are 4 squares A, B, C, D. , which make up a succession hierarchy

When the program starts, the object is created from class D and test < Strong> D class. This method calls the C class caller method. This method attempts to use the function to call the function pointer to call the correct f method. In this case it should call the f method related to the D class, but it calls the one connected to the B class.

How come?

  square A {public: virtual zero (F) = 0; }; Class B: Public A {Public: Virtual Zero F () {cout & lt; & Lt; "In B" & lt; & Lt; Andal;}}; Class C: Public B {Public: Virtual Zero F () {B :: F (); Cout & lt; & Lt; "In Sea" & lt; & Lt; Endl; } Virtual Zero Collar () {Zero (A: * CB) () = Faucet; CB = & amp; A :: F; (This-> CB =); }}; Class D: Public C {Public: Virtual Zero (F) (C: F (); Cout & lt; & Lt; "In D" & lt; & Lt; Endl; } Zero testing () {collar (); }}; Int main () {D d; D.test (); // Why does it print only "INB" return 0; } :   

UPDATE : The code actually works, due to the code presented, there was no reason for the problem and it seemed that Word A provided the library The version should come from the mismatch. , B, C and Class D provide one.

Are you sure that it only prints "in b"? I compile it on MSVC2012, I got

  INB IN C IN D   

, does not compile your original code on VS 2012, at least Not before I add public : in each class eg:

  class C: public B {public: //  CB =); }};    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

jsp - Google line chart x-axis shrinks on transition -

java - Reaching JTextField in a DocumentListener -