建设网站的企业有哪些,请人帮忙做淘宝网站多少钱,中国域名管理中心,电力公司在哪个网站做推广最好关于隐藏 (hidden) 假如继承以后#xff0c;子类出现父类同名函数#xff0c; 即使参数的形式不同#xff0c; 也会导致父类的函数隐藏#xff0c; 不参与函数匹配#xff0c;不能使用。 这个链接讲的不错。https://zhuanlan.zhihu.com/p/575423511
#include iostrea…关于隐藏 (hidden) 假如继承以后子类出现父类同名函数 即使参数的形式不同 也会导致父类的函数隐藏 不参与函数匹配不能使用。 这个链接讲的不错。https://zhuanlan.zhihu.com/p/575423511
#include iostream
using namespace std;
class Vehicle {
public:void accelerate() { cout Increasing speed: ; }
};
class Aeroplane : public Vehicle {public:void accelerate(int height) {cout Accelerating at a height of: height;}
};int main() {Aeroplane plane;plane.accelerate(1000);plane.accelerate(); //这里出错main.cpp:17:25: error: no matching function for call to ‘Aeroplane::accelerate()’cout endl;
}如果子类的instance就是想调用父类的同名不同参的函数呢方法有两个。
显式调用父类版本的函数 b.A::func(); // 显式指定父类版本合法 #include using namespace std; class Vehicle { public: void accelerate() { cout Increasing speed: ; } }; class Aeroplane : public Vehicle { public: void accelerate(int height) { cout Accelerating at a height of: height; } };
int main() { Aeroplane plane; plane.accelerate(1000); plane.Vehicle::accelerate(); cout endl; }
用using using A::func; // 解开父类重载函数的默认隐藏。
#include iostream
using namespace std;
class Vehicle {
public:void accelerate() { cout Increasing speed: ; }
};
class Aeroplane : public Vehicle {public:using Vehicle::accelerate;void accelerate(int height) {cout Accelerating at a height of: height;}
};int main() {Aeroplane plane;plane.accelerate(1000);plane.accelerate();cout endl;
}当然也可以直接在子类的函数里面调用
class Aeroplane : public Vehicle {public:void accelerate(int height) {Vehicle::accelerate(); cout Accelerating at a height of: height;}
};关于overload
关于override 如果不用virtual怎么实现多态呢可以用cast。但是对不同子类的object都要cast到对应的子类类型显然很不方便。 static_castCircle *(s)-draw();
#include iostream
#include vectorusing namespace std;class Shape {
public:void draw() { cout Drawing a generic shape...\n; }
};class Circle: public Shape {
public:void draw() { cout Drawing a circle...\n; }
};int main() {vectorShape * shapes; // Vector of pointers to Shape instances// Create a pointer to a child class of Shape and append it to the vector shapes.push_back(new Circle);for (auto s: shapes)static_castCircle *(s)-draw(); for (auto s : shapes) // Release allocated memorydelete s;
}