C ++ constructor calls a virtual function consequences

#include <the iostream> class CX {
 public :
     Virtual void FUNC () { 
        STD :: COUT << " FUNC " << STD :: endl; 
    } 
    CX () { 
        FUNC (); // constructor calls a virtual function, grammatically OK, not the effect, because when the object by subclasses into the base class constructor is the base class type
         // regardless call, total calls only to the base class virtual function, virtual function can not be called into subclasses, see the following test     } 
}; class CB: public CX {
     void FUNC () { 
        STD :: COUT << " cb.func " << STD :: endl; 
    } 
}; int

 




 main()
{
    cx ox; //func
    cb ob; //func

    cx* pox = new cb(); //func
    pox->func(); //cb.func
}

 

Guess you like

Origin www.cnblogs.com/timeObjserver/p/12003563.html