c++
只有调用基类的指针和引用调用虚函数时,才会发生动态绑定。ide
Derived d;//Derived是派生类,Base是基类 Base *ptr=&d;//积累指针指向派生类的对象 Base &ref=d;//基类引用做为派生类的别名 Base b=d;//调用Base的拷贝函数用d构造b
ptr->函数名,调用派生类中的对应函数 ref.函数名,掉用派生类中的对应函数 b.函数名,调用基础类中的对应函数
经过派生类对象访问同名函数。这是静态连编函数
经过基类对象的指针访问同名函数。这也是静态连编学习
经过基类对象的指针或引用访问同名虚函数,这是动态连编(老师喜欢考的)this
class P{public:f(){.....}};//father class C:public P{public:f(){....}};//son main(){ P* ptr; P p; C c; ptr=&p; ptr->f();//调用P::f() ptr=&c; ptr->f();//调用P::f() }
注意:要是没有vitural,指针什么类型就调用什么类型spa
class A { public: virtual void foo() {} void bar() {} }; class B : public A { public: void foo() const override { // 错误: B::foo 不覆写 A::foo } // (签名不匹配) void foo() override; // OK : B::foo 覆写 A::foo void bar() override {} // 错误: A::bar 非虚 }; void B::foo() override {// 错误: override只能放到类内使用 }
class Animal { public: Animal(); ~Animal(); virtual Animal& speak() {//能够只在父类中定义vitural return *this;//注意必定要在每个覆写的后面加上这个返回 } }; class Dog:public Animal { public: Dog(); ~Dog(); Dog& speak() { cout << "wang" << endl; delete this; return *this; } }; void test(Animal& p){//注意:覆写以后暂时尚未效果,必需要定义一个void函数来调用,固然是用下面的方式来调用啦 p.speak(); }
struct Base { virtual void foo(); }; struct A : Base { void foo() final; // A::foo 被覆写且是最终覆写 void bar() final; // 错误:非虚函数不能被覆写或是 final }; struct B final : A // struct B 为 final,不能被继承 { void foo() override; // 错误: foo 不能被覆写,由于它在 A 中是 final };
特征:指针
抽象类不能实例化。对象
抽象类能够包含抽象方法和抽象访问器。blog
从抽象类派生的非抽象类必须包括继承的全部抽象方法和抽象访问器的实际实现。继承
必须是派生类来实现,而不是让基类实现
使用状况:下面会提到上转和下转,与其相似
// A function for displaying a Shape object void printObject(Shape &shape) { cout << "The area is " << shape.getArea() << endl; //这里就是判断是否是该类型是否是要转换的类型,固然必须用到指针 Shape *p = &shape; Circle *c = dynamic_cast<Circle*>(p); // Circle& c = dynamic_cast<Circle&>(shape); // 引用转换失败则抛出一个异常 std::bad_cast if (c != nullptr) // 转换失败则指针为空 { cout << "The radius is " << p1->getRadius() << endl; cout << "The diameter is " << p1->getDiameter() << endl; } }
上转:upcasting : Assigning a pointer of a derived class type to a pointer of its base class type (将派生类类型指针赋值给基类类型指针)
Shape* s = nullptr; Circle *c = new Circle(2); s = c; //OK,隐式上转
下转:downcasting : Assigning a pointer of a base class type to a pointer of its derived class type. (将基类类型指针赋值给派生类类型指针)
Shape* s = new Circle(1); Circle *c = nullptr; c = dynamic_cast <Circle*> (s); //显式下转
好了,上面就是我对c++虚函数一些性质的简单总结和用法