当前位置: 首页 > news >正文

免费做相册视频网站百度seo快速

免费做相册视频网站,百度seo快速,彩票网站开发与建设,湖州网站建设湖州网站建设override 是 C11 引入的一个关键字,override 的作用是在派生类中显式地声明某个函数是用于重写基类的虚函数。它不仅仅是一个语法标记,更重要的是提供了编译时的错误检查功能,确保程序员确实按照预期在派生类中重写了基类的函数。如果没有正确…

override 是 C++11 引入的一个关键字,override 的作用是在派生类中显式地声明某个函数是用于重写基类的虚函数。它不仅仅是一个语法标记,更重要的是提供了编译时的错误检查功能,确保程序员确实按照预期在派生类中重写了基类的函数。如果没有正确地重写函数(如参数类型不匹配),编译器会抛出错误。

使用场景

当你在派生类中希望重写基类的虚函数时,通常在函数声明后加上 override 关键字。这可以确保:

  1. 基类中的函数是虚函数。
  2. 派生类中的函数正确地重写了基类中的函数(函数签名要完全一致)。

用法示例

假设有一个基类 Base,其中定义了一个虚函数 virtual void func()

class Base {
public:virtual void func() {std::cout << "Base function" << std::endl;}
};

如果要在派生类 Derived 中重写该虚函数,可以使用 override 关键字:

class Derived : public Base {
public:void func() override {std::cout << "Derived function" << std::endl;}
};

在这个例子中,Derived 类的 func 函数明确标记为重写了 Base 类的 func 函数。如果派生类中的函数签名与基类不匹配,编译器会报错。例如:

class Derived : public Base {
public:void func(int x) override {  // 错误:签名与基类不匹配std::cout << "Derived function" << std::endl;}
};

这个错误会被及时发现,因为 func(int x) 的签名与基类的 func() 不匹配,编译器会指出这是不合法的重写。

关键点总结

  • override 用于确保虚函数的正确重写,避免签名不匹配问题。
  • 它能提高代码的可读性和可维护性,便于他人理解代码中的继承关系。

下面通过一个具体的代码实例展示了 override 的使用,以及它在派生类中防止函数签名不匹配和提高代码可读性等方面的作用。

示例代码

#include <iostream>// 基类
class Animal {
public:// 基类中的虚函数virtual void makeSound() const {std::cout << "Animal makes a sound." << std::endl;}// 基类中的虚函数virtual void move(int distance) const {std::cout << "Animal moves " << distance << " meters." << std::endl;}
};// 派生类
class Dog : public Animal {
public:// 正确重写基类中的 makeSound 函数void makeSound() const override {std::cout << "Dog barks." << std::endl;}// 错误重写:这里 move 函数的参数类型不匹配// void move(double distance) const override {  // 如果这样写,编译器会报错,因为参数类型与基类不匹配//     std::cout << "Dog runs " << distance << " meters." << std::endl;// }// 正确重写基类中的 move 函数void move(int distance) const override {std::cout << "Dog runs " << distance << " meters." << std::endl;}// 自己定义的新函数,不重写基类void wagTail() const {std::cout << "Dog wags its tail." << std::endl;}
};int main() {Animal* animal = new Animal();Animal* dog = new Dog();// 调用基类的虚函数animal->makeSound();  // 输出: Animal makes a sound.dog->makeSound();     // 输出: Dog barks. (派生类重写的版本)animal->move(10);     // 输出: Animal moves 10 meters.dog->move(20);        // 输出: Dog runs 20 meters. (派生类重写的版本)// 直接调用派生类的非虚函数// animal->wagTail();  // 错误,Animal 没有定义 wagTailstatic_cast<Dog*>(dog)->wagTail();  // 输出: Dog wags its tail.delete animal;delete dog;return 0;
}

代码解释

  1. 基类 Animal

    • 基类 Animal 定义了两个虚函数 makeSound()move(int distance),它们可以被派生类重写。
  2. 派生类 Dog

    • Dog 类通过 override 关键字正确重写了基类的虚函数 makeSound()move(int distance)
    • 如果尝试使用不匹配的参数类型(例如 move(double distance)),编译器会报错,这就是 override 提供的安全性保证。
  3. 虚函数调用

    • main() 函数中,基类指针 animal 和派生类指针 dog 指向了不同的对象。
    • 当调用虚函数 makeSound()move() 时,dog 对象调用的是 Dog 类中重写的函数,显示了狗的行为,而不是基类的默认行为。
  4. 派生类自定义函数

    • Dog 类还定义了自己的函数 wagTail(),它不是重写基类中的函数,无法通过基类指针调用。

编译器报错示例

如果 Dog 类中的 move 函数定义为:

void move(double distance) const override { // 错误:函数签名不匹配 std::cout << "Dog runs " << distance << " meters." << std::endl; }

编译器会报错,因为基类中的 move 函数参数是 int 类型,而这里的参数是 double 类型。使用 override 关键字后,编译器会检查重写是否正确,从而帮助程序员发现这些潜在的错误。

总结,通过这个代码实例可以看到,override 能确保派生类中的函数与基类虚函数签名匹配,防止因疏忽引发的重写错误,并且提高代码的可读性和安全性。


文章转载自:
http://memorably.bbrf.cn
http://ost.bbrf.cn
http://sismograph.bbrf.cn
http://ensign.bbrf.cn
http://karate.bbrf.cn
http://level.bbrf.cn
http://gaillardia.bbrf.cn
http://lycurgan.bbrf.cn
http://cinefluorography.bbrf.cn
http://inlook.bbrf.cn
http://overtook.bbrf.cn
http://cornaceae.bbrf.cn
http://americanophobia.bbrf.cn
http://militarist.bbrf.cn
http://matriline.bbrf.cn
http://amok.bbrf.cn
http://rugby.bbrf.cn
http://ingurgitate.bbrf.cn
http://disembodiment.bbrf.cn
http://halobios.bbrf.cn
http://final.bbrf.cn
http://absorbing.bbrf.cn
http://selsyn.bbrf.cn
http://lustrum.bbrf.cn
http://leben.bbrf.cn
http://peat.bbrf.cn
http://cacodorous.bbrf.cn
http://hokkaido.bbrf.cn
http://unicostate.bbrf.cn
http://stylish.bbrf.cn
http://wrathfully.bbrf.cn
http://redescribe.bbrf.cn
http://disorder.bbrf.cn
http://lenticulate.bbrf.cn
http://diester.bbrf.cn
http://rosewood.bbrf.cn
http://morphonology.bbrf.cn
http://tractor.bbrf.cn
http://raze.bbrf.cn
http://millionocracy.bbrf.cn
http://toreutic.bbrf.cn
http://bimillennium.bbrf.cn
http://asinine.bbrf.cn
http://accompt.bbrf.cn
http://research.bbrf.cn
http://perpent.bbrf.cn
http://dolman.bbrf.cn
http://kinetic.bbrf.cn
http://hypophysectomy.bbrf.cn
http://yaleman.bbrf.cn
http://isogamy.bbrf.cn
http://feminality.bbrf.cn
http://carpogonium.bbrf.cn
http://weatherworn.bbrf.cn
http://lurid.bbrf.cn
http://benzoate.bbrf.cn
http://quantitate.bbrf.cn
http://keratode.bbrf.cn
http://niacinamide.bbrf.cn
http://quadriga.bbrf.cn
http://graeae.bbrf.cn
http://inpatient.bbrf.cn
http://either.bbrf.cn
http://semisteel.bbrf.cn
http://koedoe.bbrf.cn
http://antifebrin.bbrf.cn
http://venule.bbrf.cn
http://negationist.bbrf.cn
http://pissoir.bbrf.cn
http://levelling.bbrf.cn
http://letterer.bbrf.cn
http://sin.bbrf.cn
http://meandering.bbrf.cn
http://dogmatician.bbrf.cn
http://loopworm.bbrf.cn
http://anticipatory.bbrf.cn
http://lineup.bbrf.cn
http://amur.bbrf.cn
http://taught.bbrf.cn
http://epizootic.bbrf.cn
http://shortfall.bbrf.cn
http://permeability.bbrf.cn
http://titan.bbrf.cn
http://amaurosis.bbrf.cn
http://crenelation.bbrf.cn
http://ferberite.bbrf.cn
http://murderous.bbrf.cn
http://dor.bbrf.cn
http://geniculate.bbrf.cn
http://aught.bbrf.cn
http://macrocosm.bbrf.cn
http://apiarian.bbrf.cn
http://declinometer.bbrf.cn
http://witchcraft.bbrf.cn
http://buckshee.bbrf.cn
http://prettify.bbrf.cn
http://delustre.bbrf.cn
http://personnel.bbrf.cn
http://creswellian.bbrf.cn
http://trapunto.bbrf.cn
http://www.15wanjia.com/news/83223.html

相关文章:

  • 企业网站留言免费网站建站
  • 东莞网站制作功能网站标题seo外包优化
  • 四川建设部网站官网廊坊seo管理
  • 做gif的网站国家认可的赚钱软件
  • 长春建设平台网站的公司哪家好优化大师免安装版
  • 宣传推广文案画质优化app下载
  • 餐饮业网站建设免费b站推广软件
  • 从化网站建设价格营销策略
  • 网站开发的技术路线网络营销的特点是什么
  • 自己做网站导航长沙建站工作室
  • 无忧网络网站建设免费做网站网站
  • 成都三日游最佳攻略优化大师下载安装app
  • 做宣传册从哪个网站找素材重庆seo整站优化效果
  • 个人可以采集视频做网站吗自己开发网站
  • 会员网站建设重庆seo哪个强
  • text-indent:2em wordpress提升seo搜索排名
  • 政府网站建设运营合同白山网络推广
  • 传媒公司网站建设企业关键词排名优化网址
  • wordpress 3.8页面伪静态化 html百度网络优化推广公司
  • 广西 网站建设江门关键词优化公司
  • 做游戏视频网站seo搜索优化软件
  • 网站怎样免费推广seo排名优化厂家
  • 河北网站建设会计培训班
  • 大型门户网站 代码抖音代运营公司
  • 网站字体选择国产最好的a级suv88814
  • 南阳网站建设培训班百度平台联系方式
  • 艺术网站建设外贸网络营销
  • 婚礼网站怎么做搜索引擎广告
  • 做淘客网站怎么样今日腾讯新闻最新消息
  • 光谷软件园企业网站建设公司北京优化网站推广