C++ 重写虚函数的代码使用注意点+全部知识点
2020-12-13 02:17
标签:结果 cpp practice 其他 fine not pre fun use C++ 重写虚函数的代码使用注意点+全部知识点 标签:结果 cpp practice 其他 fine not pre fun use 原文地址:https://www.cnblogs.com/azbane/p/11031202.html#ifndef VIRTUALFUNCTION_H
#define VIRTUALFUNCTION_H
/*
*覆盖虚函数的返回值,可以是本类类型的指针或者引用,且父类可转为子类。其他覆盖虚函数,返回类型必须相同,参数类型必须相同,参数类型的顺序必须相同。
*/
class virtualFunction
{
public:
virtualFunction();
virtual virtualFunction * own();
virtual float defaultChange();
virtual void param(int, double, float);
};
class parentFunction: public virtualFunction
{
public:
parentFunction * own() override; //正确
//double defaultChange() override; //报错 conflicting return type
float defaultChange() override; //正确
//void param(double, float, int) override; //报错 maked override, but does not override
void param(int, double, float) override; //正确
};
#endif // VIRTUALFUNCTION_H
cpp----------------------------------
#include "virtualfunction.h"
#include
打印结果---------------------
i am parentFunction father...
i am father double...
i am father, my param order is int - double - float...
D:\A_XBS\Qt_project\InheritanceClassPractice\build-inhe