多重继承、虚继承与虚基类

c++对象模型05:虚继承内存布局 虚继承 虚继承解决了菱形继承中最派生类拥有多个间接父类实例的情况。虚继承的派生类的内存布局与普通继承很多不同,主要体现在: 虚继承的派生类,如果定义了新的虚函数,则编译器为其生成一个虚函数指针(vptr)以及一张虚函数表。该vptr位于对象内存最前面。(非虚继承时,派生类新的虚函数直接扩展在基类虚函数表的下面。) 虚继承的派生类有单独的虚函数表,基类也有单独的虚函数表,两部分之间用一个四个字节的0x00000000来作为分界。 虚继承的派生类对象中,含有四字节的虚基表指针。 在C++对象模型中,虚继承而 阅读详情

多重继承

单重继承:一个派生类最多只能有一个基类
多重继承:一个派生类可以有多个基类

class 类名: 继承方式 基类1,继承方式 基类2,…. {….};

多个基类中可能包含同名变量或函数给派生类造成二义性。多重继承中可以使用基类名::数据成员名解决访问歧义的问题。

#include <iostream>
using namespace std;

class Bed
{
public:
    Bed(int weight) : weight_(weight)
    {

    }
    void Sleep()
    {
        cout << "Sleep ..." << endl;
    }
    int weight_;
};

class Sofa
{
public:
    Sofa(int weight) : weight_(weight)
    {

    }
    void WatchTV()
    {
        cout << "Watch TV ..." << endl;
    }
    int weight_;
};

class SofaBed : public Bed, public Sofa
{
public:
    SofaBed() : Bed(0), Sofa(0)
    {
        FoldIn();
    }
    void FoldOut()
    {
        cout << "FoldOut ..." << endl;
    }
    void FoldIn()
    {
        cout << "FoldIn ..." << endl;
    }
};

int main(void)
{
    SofaBed sofaBed;
    //sofaBed.weight_ = 10; error
    //sofaBed.weight_ = 20; error

    sofaBed.Bed::weight_ = 10;
    sofaBed.Sofa::weight_ = 20;

    sofaBed.WatchTV();
    sofaBed.FoldOut();
    sofaBed.Sleep();

    return 0;
}

不能直接写 sofaBed.weight_ = 10;因为sofaBed 继承了Sofa 和 Bed ,实际上有weigh_的两份拷贝,这样指向不明。只能通过sofaBed.Bed::weight_ = 10;访问,但实际上一个sofaBed理应只有一个weight_,下面通过虚基类和虚继承可以解决这个问题。

虚继承与虚基类

虚基类用来解决多重继承时可能发生的对同一基类继承多次而产生的二义性问题,同时为最远的派生类提供唯一的基类成员,而不重复产生多次拷贝。

#include <iostream>
using namespace std;

class Furniture
{
public:
    Furniture(int weight) : weight_(weight)
    {
        cout << "Furniture ..." << endl;
    }
    ~Furniture()
    {
        cout << "~Furniture ..." << endl;
    }
    int weight_;
};

class Bed : virtual public Furniture
{
public:
    Bed(int weight) : Furniture(weight)
    {
        cout << "Bed ..." << endl;
    }
    ~Bed()
    {
        cout << "~Bed ..." << endl;
    }
    void Sleep()
    {
        cout << "Sleep ..." << endl;
    }

};

class Sofa : virtual public Furniture
{
public:
    Sofa(int weight) : Furniture(weight)
    {
        cout << "Sofa ..." << endl;
    }
    ~Sofa()
    {
        cout << "~Sofa ..." << endl;
    }
    void WatchTV()
    {
        cout << "Watch TV ..." << endl;
    }
};

class SofaBed : public Bed, public Sofa
{
public:
    SofaBed(int weight) : Bed(weight), Sofa(weight), Furniture(weight)
    {
        cout << "SofaBed ..." << endl;
        FoldIn();
    }
    ~SofaBed()
    {
        cout << "~SofaBed ..." << endl;
    }
    void FoldOut()
    {
        cout << "FoldOut ..." << endl;
    }
    void FoldIn()
    {
        cout << "FoldIn ..." << endl;
    }
};

int main(void)
{
    SofaBed sofaBed(5);
    sofaBed.weight_ = 10;


    sofaBed.WatchTV();
    sofaBed.FoldOut();
    sofaBed.Sleep();

    return 0;
}

此时只有一份weigh_,不存在访问歧义的问题。

从输出可以总结出:

1、虚基类的成员是由最远派生类的构造函数通过调用虚基类的构造函数进行初始化的。
2、在整个继承结构中,直接或间接继承虚基类的所有派生类,都必须在构造函数的成员初始化表中给出对虚基类的构造函数的调用。如果未列出,则表示调用该虚基类的默认构造函数。
3、在建立对象时,只有最远派生类的构造函数调用虚基类的构造函数,该派生类的其他基类对虚基类构造函数的调用被忽略。

C/C++杂记 虚函数的实现的基本原理(图文) 1. 概述 简单地说,每一个含有虚函数(无论是其本身的,还是继承而来的)的类都至少有一个与之对应的虚函数表,其中存放着该类所有的虚函数对应的函数指针。例: 其中: B的虚函数表中存放着B::foo和B::bar两个函数指针。 D的虚函数表中存放的既有继承自B的虚函数B::foo,又有重写(override)了基类虚函数B::bar的D::bar,还有新增的虚函数D::quz。 提示:为了描述方便,本文在探讨对象内存布局时,将忽略内存对齐对布局的影响。 2. 虚函数表构造过程 从编译器的角度来说,B的虚函数表很好构造,D的虚函数表构造过程相对复杂。下面给出了构造D的虚函数表的一种方式(仅供参考 立即下载

相关推荐

STM32内存大小与地址的对应关系以及计算方法

1、概述 在计算机中要访问存储区,都是通过地址来进行访问的,包括所有的外设(比如串口、内存、硬盘等),都对应有一个访问地址,地址就相当于门牌号,有了门牌号,我们才知道操作的是哪一个区域、哪一个外设。 最被大家熟知的是计算机内存,计算机内存有4G、8G,16G等等,对于程序员而言,要在代码中操作这些内存,肯定得知道内存的地址,此外,还需要会计算地址与内存大小的计算方法与对应关系。STM32作为微

Ace's Blog 4万+

c++-多继承与虚基类

派生类只产生一个虚基类子对象,对于虚基类,其构造函数调用基类的构造函数,由于派生类对象中只有一个虚基类子对象,所以在建立 一个派生 类的对象时,虚其类构造函数只能被调用一次,这样虚基类子对象 只被初始化一次。如果派生类定义了一个和某个 基类成员同名的新成员 ,派生 的新成员产改写了 外层同名成员,不使用作用域符只能访问派生 类的这个成员。当一个派生类从多个基类派生 ,这些基类又一个共同的基类,对该基类中说明的成员进行访问,可能出现二义性,虚基类可解决此类问题。对象名.基类名::成员名(参数表)

The research on computer technolog 514

RK-XCKU5P RoCEv2 传感器桥工程分析报告

烧入后这块 FPGA 就是一个固定身份(CA:FE:C0:FF:EE:00 / 192.168.0.2 / 无 FEC 100G)的RoCEv2 UC 端点,每秒在 UDP 12267 广播自报家门;对端 CX5 只要关 FEC、配 192.168.0.101 +MTU 4200,就能 ping 通它、用 UDP 8192 读写它的全部寄存器、再按"ECB 旁路交换 QPN/rkey"的方式建立 UC RDMA 通道收发数据。

eloudy的专栏 257

C++:使用虚基类解决多重继承中的二义性问题

#pragma once #include <iostream> using namespace std; class Person { public: Person(char *name, bool sex); ~Person(); public: void SetName(char *name); //设置姓名 char* Getname(); //获取姓名 void SetSex(bool sex); //设置性别 bool GetSex(); //获取.

qq_41708281的博客 771

SAP MRKO与MIRO的对比分析:为何寄售结算需要定制开发?

本文深入分析了SAP系统中MRKO与MIRO在寄售结算中的功能差异,揭示了标准MRKO在财务控制和业务流程上的局限性。通过定制开发实践,如BAdI增强和界面优化,企业可显著提升结算效率与准确性。文章还探讨了未来SAP S/4HANA在寄售结算中的智能化趋势,为相关企业提供技术参考。

weixin_29300241的博客 444

使用Android GPS api 实现一个轨迹记录的小功能

引入 市场上的跑步软件已经是非常多了,如咕咚和悦跑圈还有其他,很多大厂也在涉足这个方面。而最简单的运动跑步,最近几年也因简单,不限场地,运动效果出众备受大众喜欢。 我的这篇文章的内容很多也是从网上学习其他的博客内容,外加利用android自身的提供的api实现的,希望能对有这方面需求的朋友有所帮助。 介绍 先上两张图,界面比较丑,不过一些数据基本都有了,这个图片是临时要用截取的,所以轨迹线很短,之前

尝试着去了解代码 1万+

虚基类解决多继承同名二义性

之前提过关于公有多继承存在同名二义性问题,所以c++才会有虚基类来解决这类的问题。 虚基类继承,使得基类的多个虚基继承派生类中的同名成员,存在于相同的地址,也就是同一个东西,所以当他们共同有一个派生类时,这个派生类所继承的不是俩个同名成员,其实只有一个,故不发生二义性问题 构造调用顺序:先调用base(调用直接基类时,直接基类是由base继承过来),接着根据base3的继承申明,依次调用基类,从结果可知确实如此;析构顺序相反 #include<iostream> using namespac.

ID__10086的博客 663

ubuntu18.04在carla中导入Roadrunner自建的地图

进入Carla根目录下的Import文件夹在Import文件夹下面新建一个文件夹 这里我取名为RoadA进入新建的文件夹,创建json文件,json文件的名字与文件夹名字相同再将从RoadRunner中导出的地图文件夹(map)放入新建的文件夹目录下重点关注.fbx和.xodr文件然后打开新建的json文件添加并修改如下代码name为RoadRunner中导出的地图文件夹名字。

justinyjf的博客 1905

【C++面向对象程序设计】CH5 继承与派生(续)——虚基类

继承与派生(续)——虚基类介绍

m0_72318954的博客 1667

ITIL 4 Foundation 自学通关笔记1

ITIL 4 Foundation 自学通关笔记

m0_58817196的博客 964

ABAP Development Tool for Visual Studio Code 里使用 MCP server

本文讨论了SAP ABAP开发环境中,除GitHub Copilot外,其他可作为MCP客户端接入ABAP开发工具(MCP server)的编码代理(coding agent)。GitHub Copilot是官方确认可用的工具,而其他代理如Claude Code、Cursor、Windsurf Cascade、Cline和Continue虽然理论上支持MCP协议,但要实际用于ABAP开发还需满足两个关键条件:兼容MCP协议,并能处理VS Code虚拟工作区文件系统。这些工具需要单独验证是否支持ABAP开发工

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 275

C++语法——详解虚继承

一.什么是虚继承所谓虚继承(virtual)就是子类中只有一份间接父类的数据。该技术用于解决多继承中的父类为非虚基类时出现的数据冗余问题,即菱形继承问题。,那么,当我们用D实例化对象调用a时,编译会报错,因为发生了混淆。除非指定类域B或C。当然指定A也可以,因为默认会从第一个父类中找。此时,D的实例化对象内部结构如下:而当我们使用虚继承时,结构是下图这样,D中只有一份父类A,当我们调用A中数据时,并不会发生冗余。

weixin_61857742的博客 3万+

AUTOSAR从入门到精通-【应用篇】基于AUTOSAR架构的混合动力电池 ECU 软件开发

由于严峻的环境污染和能源短缺问题,各国都在致力于发展新能源车,以达到节能排放的目的。在此背景下,12V启停系统由于成本较低廉、匹配方便,得到广泛应用。但是12V系统作为启停系统后存在许多问题,比如节能和减排效果有限,并且轻度混合动力系统的功率需求通常在10~15KW,在此电压条件下运行时,电池的电流会达到1000A,因此该方案行不通。高压混合动力系统(>110V)的节油效果好,但是成本很高,并且需要大功率的电机。48V混合动力系统比高压混合动力系统成本。

getusushu的博客 1495

C++ 多重继承、虚继承与虚基类

前言:虚继承是为了解决多重继承出现菱形继承时出现的问题。例如:类B、C分别继承了类A。类D多重继承类B和C的时候,类A中的数据就会在类D中存在多份。通过声明继承关系的时候加上virtual关键字可以实现虚继承。 一、多重继承 单重继承——一个派生类最多只能有一个基类 多重继承——一个派生类可以有多个基类 class 类名: 继承方式 基类1,继承方式 基类2,…. {….}; 派生类同...

Magnum的博客 3909

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

深度学习工具包 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")

C++继承与多态六:虚基类、虚继承、多重继承中菱形继承问题及解决方案

文章目录一、虚基类和虚继承二、虚基类和虚继承出错情况分析三、菱形继承问题 一、虚基类和虚继承 虚基类:被虚继承的类,就称为虚基类。 virtual作用: 1.virtual修饰了成员方法是虚函数。 2.可以修饰继承方式,是虚继承。被虚继承的类就称为虚基类。 vfptr:一个类有虚函数,这个类生成的对象就有vfptr,指向vftable。 vbptr:在派生类中从基类虚继承而来。 vftable:第...

Rain的博客 797

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

从零开始学C++之继承(三):多重继承、虚继承与虚基类

一、多重继承 单重继承——一个派生类最多只能有一个基类 多重继承——一个派生类可以有多个基类 class 类名: 继承方式 基类1,继承方式 基类2,…. {….}; 派生类同时继承多个基类的成员,更好的软件重用 可能会有大量的二义性,多个基类中可能包含同名变量或函数 多重继承中解决访问歧义的方法: 基类名::数据成员名(或成员函数(参数表)) 明确指明

Meditation 3480

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

(P32)继承:多重继承 ,虚继承与虚基类 ,虚基类及其派生类构造函数

文章目录1.多重继承2.虚继承与虚基类3.虚基类及其派生类构造函数 1.多重继承 类图 Bed类继承至Furniture类 2.虚继承与虚基类 当派生类从多个基类派生,而这些基类又从同一个基类派生,则在访问此共同基类中的成员时,将产生二义性——采用虚基类来解决。 eg:P32\02.cpp中,SofaBed类应该只有一个weight_,也不应该有2个weight_ 虚基类的引入 用于有共同基类的场合 声明 以virtual修饰说明基类 class B1: virtual public BB 等价于 cl

喜欢打篮球的普通人 503

C++基础 多重继承引发的 虚基类与虚继承(基类被虚继承 就称为虚基类?)

//-- 不采用虚基类的方式 classA.h #pragma once #include "grand.h" class A :public Grand { public: A(int a, int b, int c); ~A(); void testTongMingHanShu(); int m_iTingMIngBianLiang; private: }; A::A(int a, int b, int c) :Grand(a) { printf("A的构造a= %d\n", a); }

baidu_19552787的博客 158

二十七、继承(四) 多重继承、虚继承与虚基类、虚基类及其派生类构造函数

一、多重继承           在现实生活中,经常会有一个对象属于多个类别的情况,比如沙发床同时具有沙发和床的特点,这个时候我们可以使用多重继承来模拟这种关系。例子如下: #include using namespace std; class Furniture { public: Furniture(int weight) : weight_(weight) { } int

tianttt的专栏 821

【C++】多重继承与虚继承

虚继承可以解决二义性和A被构建多次这两个问题,普通继承不能解决;虚继承通过增加一个指针(浪费了一点存储空间),换取了更高的效率只要一个类虚继承了其它类,那么该类所有的对象中都会新增一个指针,该指针专门用来指向系统中虚基类表的首地址。

qq_62917398的博客 1255

[C++基础]虚继承实现原理

在C++中,我们会遇到virtual这个关键字,但是它有两种含义:虚函数和虚继承,它们两个是完全无相关的两个概念。 什么是虚继承 虚继承是解决C++多重继承问题的一种手段,从不同途径继承来的同一基类,会在子类中存在多份拷贝。这将存在两个问题:其一,浪费存储空间;第二,存在二义性问题,通常可以将派生类对象的地址赋值给基类对象,实现的具体方式是,将基类指针指向继承类(继承类有基类的拷贝)中的基...

超越 1533

【C++】继承和派生、虚继承和虚基类、虚基类表和虚基类指针

继承和派生、虚继承和虚基类、虚基类表和虚基类指针继承和派生继承概述继承基本概念派生类中的成员继承的内容派生类定义派生类访问控制对象构造和析构对象构造和析构的调用顺序继承中的构造和析构的调用规则调用子类构造前会先调用父类构造继承中同名成员的处理方法继承中的静态成员特性非自动继承的函数多重继承虚继承和虚基类菱形继承虚继承和虚基类虚继承(Virtual Inheritance)虚基类(Virtual Base Class)继承后虚基类成员的可见性虚基类表和虚基类指针 继承和派生 继承概述 继承基本概念 c++最重

这道题我不会做...不会做啊..太难了!! 4095

C++(20):多重继承与虚继承

C++ 知识点复习,《C++ primer》

学习记录 374
上一篇: leetcode33. 搜索旋转排序数组
下一篇: C++多态虚函数表详解(多重继承、多继承情况)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址