C++中Override

C++11中override关键字使用 什么是override? override 是C++11引进的一个说明符,翻译为 覆盖 的意思。C++11 中的 override 关键字,可以显式的在派生类中声明,哪些成员函数需要被重写,如果没被重写,则编译器会报错。 override有什么用? 在我们C++编程过程中,最熟悉的就是对接口方法的实现,在接口中一般只是对方法进行了声明(declaration),而我们在实现时,就需要实现接口声明的所有方法。 在程序中加override 关键字,可以避免派生类中忘记重写虚函数的错误。 另一个典型.. 阅读详情

1  公有继承

  公有继承包含两部分:一是 "接口" (interface),二是 "实现" (implementation)

  基类 Shape 中,三个成员函数,代表三种继承方式:

复制代码
class Shape {
public:
    virtual void Draw() const = 0;    // 1) 纯虚函数
    virtual void Error(const std::string& msg);  // 2) 普通虚函数
    int ObjectID() const;  // 3) 非虚函数
};

class Rectangle: public Shape { ... };
class Ellipse: public Shape { ... };
复制代码

1.1  纯虚函数 (pure virtual)

  纯虚函数,表示继承的只是基类成员函数的接口,且要在派生类中重写该函数的实现

Shape *ps1 = new Rectangle;
ps1->Draw(); // calls Rectangle::Draw

Shape *ps2 = new Ellipse;
ps2->Draw(); // calls Ellipse::Draw

  若想调用基类的 Draw(),须加上 类作用域操作符 ::

ps1->Shape::Draw(); // calls Shape::draw

1.2  普通虚函数

  普通虚函数,对应在基类中定义一个缺省的实现 (default implementation),表示继承的是基类成员函数的接口和缺省的实现,由派生类自行选择是否重写该函数。

  实际上,允许普通虚函数同时继承接口和缺省实现是危险的。 如下, ModelA 和 ModelB 是 Airplane 的两种飞机类型,且二者的飞行方式完全相同

class Airplane {
public:
    virtual void Fly(const Airport& destination);
};
class ModelA: public Airplane { ... };
class ModelB: public Airplane { ... };

  这是典型的面向对象设计,两个类共享一个特性 -- Fly,则 Fly 可在基类中实现,并由两个派生类继承之

  现增加一个新的飞机型号 ModelC,其飞行方式与 ModelA,ModelB 并不相同,假如不小心忘了在 ModelC 中重写新的 Fly 函数

class ModelC: public Airplane {
    ... // no fly function is declared
};

  则调用 ModelC 中的 fly 函数,就是调用 Airplane::Fly,但是 ModelC 的飞行方式和缺省的并不相同

Airplane *pa = new ModelC;
pa->Fly(Qingdao); // calls Airplane::fly!

  这就是前面所说的,普通虚函数同时继承接口和缺省实现是危险的,最好是基类中实现缺省行为 (behavior),但只有在派生类要求时才提供该缺省行为

1.2.1  解决方法一 

  一种方法是 纯虚函数 + 缺省实现,因为是纯虚函数,所以只有接口被继承,其缺省的实现不会被继承。派生类要想使用该缺省的实现,必须显式的调用

复制代码
class Airplane {
public:
    virtual void Fly(const Airport& destination) = 0;
};

void Airplane::Fly(const Airport& destination)
{ 
    // a pure virtual function default code for flying an airplane to the given destination
}

class ModelA: public Airplane {
public:
    virtual void Fly(const Airport& destination) { Airplane::Fly(destination); }
};
复制代码

  这样在派生类 ModelC 中,即使一不小心忘记重写 Fly 函数,也不会调用 Airplane 的缺省实现

复制代码
class ModelC: public Airplane {
public:
    virtual void Fly(const Airport& destination);
};

void ModelC::Fly(const Airport& destination)
{
    // code for flying a ModelC airplane to the given destination
}
复制代码

1.2.2  解决方法二 

  可以看到,上面问题的关键就在于,一不小心在派生类 ModelC 中忘记重写 fly 函数,C++11 中使用关键字 override,可以避免这样的“一不小心”

1.3  非虚函数

  非虚成员函数没有 virtual 关键字,表示派生类不但继承了接口,而且继承了一个强制实现 (mandatory implementation)

  既然继承了一个强制的实现,则在派生类中,无须重新定义 (redefine) 继承自基类的成员函数,如下:

  使用指针调用 ObjectID 函数,则都是调用的 Shape::ObjectID()

复制代码
Rectangel rc; // rc is an object of type Rectangle

Shape *pB = &rc; // get pointer to rc
pB->ObjectID(); // call ObjectID() through pointer

Rectangle *pD = &rc; // get pointer to rc
pD->ObjectID(); // call ObjectID() through pointer
复制代码

  如果在派生类中重新定义了继承自基类的成员函数 ObjectID 呢?

复制代码
class Rectangel : public Shape {
public:
    int ObjectID() const; // hides Shape::ObjectID
};

pB->ObjectID(); // calls Shape::ObjectID()
pD->ObjectID(); // calls Rectagle::ObjectID()
复制代码

  此时,派生类中重新定义的成员函数会 “隐藏” (hide) 继承自基类的成员函数

  这是因为非虚函数是 “静态绑定” 的,pB 被声明的是 Shape* 类型的指针,则通过 pB 调用的非虚函数都是基类中的,既使 pB 指向的是派生类

  与“静态绑定”相对的是虚函数的“动态绑定”,即无论 pB 被声明为 Shape* 还是 Rectangle* 类型,其调用的虚函数取决于 pB 实际指向的对象类型

 

2  重写 (override)

  在 1.2.2 中提到 override 关键字,可以避免派生类中忘记重写虚函数的错误

  下面以重写虚函数时,容易犯的四个错误为例,详细阐述之

复制代码
class Base {
public:
    virtual void mf1() const;
    virtual void mf2(int x);
    virtual void mf3() &;
    void mf4() const;    // is not declared virtual in Base
};

class Derived: public Base {
public:
    virtual void mf1();        // declared const in Base, but not in Derived.
    virtual void mf2(unsigned int x);    // takes an int in Base, but an unsigned int in Derived
    virtual void mf3() &&;    // is lvalue-qualified in Base, but rvalue-qualified in Derived.
    void mf4() const;        
};
复制代码

  在派生类中,重写 (override) 继承自基类成员函数的实现 (implementation) 时,要满足如下条件:

  一虚:基类中,成员函数声明为虚拟的 (virtual)

  二容:基类和派生类中,成员函数的返回类型和异常规格 (exception specification) 必须兼容

  四同:基类和派生类中,成员函数名、形参类型、常量属性 (constness) 和 引用限定符 (reference qualifier) 必须完全相同

  如此多的限制条件,导致了虚函数重写如上述代码,极容易因为一个不小心而出错

  C++11 中的 override 关键字,可以显式的在派生类中声明,哪些成员函数需要被重写,如果没被重写,则编译器会报错。

复制代码
class Derived: public Base {
public:
    virtual void mf1() override;
    virtual void mf2(unsigned int x) override;
    virtual void mf3() && override;
    virtual void mf4() const override;
};
复制代码

  这样,即使不小心漏写了虚函数重写的某个苛刻条件,也可以通过编译器的报错,快速改正错误

复制代码
class Derived: public Base {
public:
    virtual void mf1() const override;  // adding "virtual" is OK, but not necessary
    virtual void mf2(int x) override;
    void mf3() & override;
    void mf4() const override; 
}; 
复制代码

 

小结:

1)  公有继承

  纯虚函数      => 继承的是:接口 (interface)

  普通虚函数   => 继承的是:接口 + 缺省实现 (default implementation)

  非虚成员函数 =>继承的是:接口 + 强制实现 (mandatory implementation)

2)  不要重新定义一个继承自基类的非虚函数 (never redefine an inherited non-virtual function)

3)  在声明需要重写的函数后,加关键字 override

 

参考资料:

 <Effective C++_3rd> item 34, item 36

 <Effective Modern C++> item 12

 

【C++11保姆级教程】final和override 在C++编程中,我们常常需要使用继承和多态来构建复杂的程序。然而,在继承关系中,有时候我们需要对继承层次做出一些限制,确保某些类或函数不能再被继承或重写。为了解决这个问题,C++11引入了两个关键字,分别是final和override。本篇教程将向大家介绍final和override的具体用法,帮助大家更好地理解和应用这两个关键字。final和override关键字在C++中提供了对类继承和函数重写行为的额外控制。 阅读详情

相关推荐

树莓派4B系统安装及SSH远程控制(简单易操作)

​自从2022年4月树莓派官网更新了RaspberryPiImager版本v1.7.2后,便不再支持自行建立ssh文件,并用默认用户名pi和密码raspberry的方式进行远程控制(亲测不行,输密码时一直access denied)。但是该版本软件提供了烧录系统前设置ssh用户及密码功能。本文记录了系统安装和SSH远程操作步骤。........................

机械人的代码路 5789

C++11 之 override

1 公有继承 公有继承包含两部分:一是"函数接口" (interface),二是"函数实现" (implementation) 如 Shape 类中,三个成员函数,表示三种继承方式: class Shape { public: virtual void Draw() const = 0; // 1) 纯虚函数 virtual void Error...

weixin_30527323的博客 616

Android手机免root玩转Kali NetHunter:Termux保姆级安装指南(附Kex VNC配置)

本文提供了一份详细的Android手机免root安装Kali NetHunter的保姆级指南。通过使用Termux终端模拟器,用户无需解锁Bootloader或获取root权限,即可在普通安卓设备上部署完整的Kali Linux渗透测试环境。教程涵盖了从环境准备、NetHunter核心安装到配置Kex VNC实现图形桌面访问的全过程,并附带了电脑投屏与常见问题解决方案,旨在打造一个便携的移动安全学习与测试平台。

weixin_29208327的博客 293

【Note】C++ 函数后加 const override 的用法

1. const 用法 const加在函数前表示函数返回值为const,加在函数后表示不可修改class的成员变量。 const加在函数后面表示此函数“只读”。 2. override用法 override关键字表示重写父类的虚函数。 1. 表示重写了父类的函数增加可读性 2. 编译器会检查重写的函数与父类的虚函数是否一致 Example: class Base { public: virtual void Test(int number) const; }; class Oba

qq_35516517的博客 1万+

实战教程:如何用Python+小波变换+CNN搞定医学影像中的条纹噪声(附完整代码)

本文提供了一套完整的实战方案,指导如何利用Python结合小波变换与卷积神经网络(CNN)去除医学影像中的条纹噪声。该方法通过小波变换精准定位噪声,再使用轻量化CNN进行针对性清除,有效保护图像细节,并附有可运行的完整代码,为医学影像处理与AI分析提供高效工具。

weixin_29056101的博客 268

svn 分支(branch)和标签(tag)管理

svn的分支管理

caohongxing的博客 3万+

c++中 override用法

描述:override保留字表示当前函数重写了基类的虚函数。 目的: 1.在函数比较多的情况下可以提示读者某个函数重写了基类虚函数(表示这个虚函数是从基类继承,不是派生类自己定义的); 2.强制编译器检查某个函数是否重写基类虚函数,如果没有则报错。 用法:在类的成员函数参数列表后面添加该关键字既可。 例子: class Base { virtual void f(); }; class Deriv...

qq_43026520的博客 7270

Vue3 - Element Plus 日期选择器 el-date-picker,设置 type=week 变成周选择器后,将选择周时修改为 “周一至周日“ 为一组范围(详细示例代码,新手小白秒会)

vue3,element-plus,DatePicker日期选择器,el-date-picker,周,type=week,周选择器,日期选择器week怎么拿到具体的时间范围,如何取week周选择器的value值,format如何设置,时间选择器按周选择时展示具体日期范围,vue3elementPlus日期选择器选择一周7天的数据格式怎么拿到,el-date-picker设置type等于week后怎么设置format属性,format和value-format怎么设置,范围选择器周,一周,选择一周7天时间

🏆 前端领域优质创作者 1万+

C++之override

C++ 之 override 作用 不使用override 使用override 作用 在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数。 位置:函数调用运算符之后,函数体或纯虚函数标识 “= 0” 之前。 不使用override #include "pch.h" #include <iostream> using namespace ...

枫影竹韵的博客 1756

验证 helm Chart 的正确性

验证 helm Chart 的正确性

小橙子的笔记屋 281

停车位检测:基于YOLOv5与UI界面的实现

YOLOv5是YOLO(You Only Look Once)系列中的最新版本,它能够在单次前向传播中同时完成多个目标的定位与分类,因此被广泛应用于实时目标检测任务。与之前的版本相比,YOLOv5在精度和速度上得到了很大的优化,适合嵌入到实际的自动驾驶和智能交通系统中。YOLOv5使用了卷积神经网络(CNN)进行特征提取,并通过回归方法预测边界框、类别及置信度。高效的推理速度:YOLOv5采用了优化的模型结构,能够在保证精度的同时提供较快的推理速度。较高的精度。

YOLO 694

C++ 关于override关键字使用详解

因此,只要函数签名相同,派生类的函数就可以覆盖基类的函数,无论这两个函数的实现是否相同。这就是前面所说的,普通虚函数同时继承接口和缺省实现是危险的,最好是基类中实现缺省行为 (behavior),但只有在派生类要求时才提供该缺省行为.一种方法是 纯虚函数 + 缺省实现,因为是纯虚函数,所以只有接口被继承,其缺省的实现不会被继承。这是因为非虚函数是 “静态绑定” 的,p1被声明的是 Person* 类型的指针,则通过 p1调用的非虚函数都是基类中的,既使 指向的是派生类。

nihao_2014的博客 1万+

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

C++ 中提供的override 关键字

C++ 语言标准这些年进化的很快,许多新引入的特性我都没有用过。说实话自己也比较懒,总是觉得自己对C++ 的掌握程度已经足够应付日常的各种项目了,所以没有学习新特性的动力。而且一直觉得新引入的特性多数都属于那种“语法糖”,能够发挥的作用不大,其实是可有可无的。 不过最近编写一个小程序时代码里有个小bug,困扰了我好几天。最后在几个热心网友的帮助下才解决了。这个小bug让我认识到C++11 中提供

Ivan 的专栏 4万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

C++11 override

C++11 之 override作用不使用override使用override 作用 在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数。 位置:函数调用运算符之后,函数体或纯虚函数标识 “= 0” 之前。 不使用override #include &amp;lt;iostream&amp;gt; using namespace std; class Base { public...

wuj的专栏 2万+

C++练习之override操作

C++中override(覆盖)操作符是类封装中应用到最基本的操作,琢磨了下,才算是懂了点点,以下是自己的一些理解,如有不对之处,请指正,谢谢! 源码下载地址:git clone https://laneyu@bitbucket.org/laneyu/cplusplus.git sample目录下既是本文所有例子测试代码 一、operator+操作 operator+操作主要有四种方式:

coderlang 1703

c++的virtual和override作用

一、概述 c++的virtual提供了运行时的多态,是的可以用基类的指针调用子类对象的函数,通过override可以强制要求基类有对应的虚函数。 二、测试代码 #pragma once #include <stdio.h> class Base { public: void A_1() { printf("Base::A_1\n"); }; virtual void A...

游戏猫的博客 6954

C++ 中override的作用

描述:override保留字表示当前函数重写了基类的虚函数。 目的:1.在函数比较多的情况下可以提示读者某个函数重写了基类虚函数(表示这个虚函数是从基类继承,不是派生类自己定义的);2.强制编译器检查某个函数是否重写基类虚函数,如果没有则报错。 用法:在类的成员函数参数列表后面添加该关键字既可。 例子: class Base { virtual void f(); };

qq_33485434的博客 6万+

C++ override 关键字用法

override关键字作用: 如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译。 举例子说明struct Base { virtual void Turing() = 0; virtual void Dijkstra() = 0; virtual void VNeumann(int g) = 0;

小黑霸气 的专栏 11万+

C++中override关键字

本文转自:https://blog.youkuaiyun.com/xiaoheibaqi/article/details/51272009 override关键字作用: 如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译。 举例子说明 struct Base { virtual void Turing() = 0; ...

qq_16334327的博客 895

C++ 继承之Override

C++继承之Override1.共有继承1.1纯虚函数1.2普通虚函数1.2.1 方法一1.2.2方法二1.3非虚函数2.重写(override)3.小结 1.共有继承 公有继承包含两部分:一是 “函数接口” (interface),二是 “函数实现” (implementation) 如 Shape 类中,三个成员函数,表示三种继承方式: class Shape { public: vi...

language_zcx的博客 2240

C++-[override]关键字使用详解

本文介绍了C++ override关键字使用详解以及与重载的区别。

沐槿的博客 5万+

【C++关键字 override】

如果派生类重载了基类中的同名函数,则该函数将成为派生类中的虚函数,可以通过调用基类中的函数来实现该函数的功能。因此,在C++中,override关键字是非常重要的一个特性,对于实现灵活的类型重载和抽象函数的使用非常有帮助。这可能会导致一些错误,例如如果派生类中的函数签名与基类中的虚函数不完全匹配,那么这个函数可能不会被视为重写,而编译器也不会报错。它可以帮助程序员避免一些常见的错误,例如误打误写导致的函数签名不匹配,以及在基类中添加或修改虚函数时忘记更新派生类中的函数等。

zwh1298454060的博客 752

C++中virtual和override的作用

C++中virtual和override的作用 C++的virtual提供了运行时的多态(后绑定),可以用基类的指针调用子类对象的函数。 通过override可以强制要求基类有对应的虚函数。 使用原则 基类函数没加virtual,子类有相同函数,实现的是覆盖。用基类指针调用时,调用到的是基类的函数;用子类指针调用时,调用到的是子类的函数。 基类函数加了virtual时,实现的时重写。用基类指针或子类指针调用时,调用到的都是子类的函数。 函数加上override,强制要求基本相同函数需要是虚函数,否

quietbxj的博客 6872
上一篇: C++11特性之override和final关键字
下一篇: C++中static、const、static const修饰变量作用详解
imJaron
博客等级 码龄11年 121粉丝 286原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值