首页 > 代码库 > c++知识点总结--静态与动态类型

c++知识点总结--静态与动态类型

对象的静态类型(static type),就是它在程序中被声明时所采用的类型
对象的动态类型(dynamic type)是指“目前所指对象的类型”
 
vitual 函数是动态绑定而来,调用一个virtual函数时,究竟调用哪一个函数实现,取决于发出调用的那个对象的动态类型
class Shape{    public:        enum ShapeColor{ Red, Green, Blue = 1, Org };        void printColor(){            cout << Red << Green << Blue << Org << endl;        }        virtual void draw(ShapeColor color = Red)const = 0;    };    class Rectangle :public Shape{    public:        virtual void draw(ShapeColor color = Green) const{            cout << "this is rectangle,color is " << color << endl;        }    };    class Circle :public Shape{    public:        virtual void draw(ShapeColor color)const{            cout << "this is circle,color is " << color << endl;        }    };    static void test(){        Shape *ps;        Shape *pc = new Circle;        Shape *pr = new Rectangle;        pc->draw();        pr->draw();    }默认参数是静态绑定,pr输出值仍然是Shape的默认值

 

c++知识点总结--静态与动态类型