virtual base classes
virtual base classes用来实现菱形继承解决多个重复subobject的问题
//: C09:VirtualBase.cpp
// Shows a shared subobject via a virtual base.
#include <iostream>
using namespace std; class Top
{
protected:
int x;
public:
Top(int n)
{
x = n;
}
virtual ~Top() {}
friend ostream&
operator<<(ostream& os, const Top& t)
{
return os << t.x;
}
}; class Left : virtual public Top
{
protected:
int y;
public:
Left(int m, int n) : Top(m)
{
y = n;
}
}; class Right : virtual public Top
{
protected:
int z;
public:
Right(int m, int n) : Top(m)
{
z = n;
}
}; class Bottom : public Left, public Right
{
int w;
public:
Bottom(int i, int j, int k, int m)
: Top(i), Left(0, j), Right(0, k)
{
w = m;
} friend ostream&
operator<<(ostream& os, const Bottom& b)
{
return os << b.x << ',' << b.y << ',' << b.z
<< ',' << b.w;
}
}; int main()
{
Bottom b(1, 2, 3, 4);
cout << sizeof b << endl;
cout << b << endl;
cout << static_cast<void*>(&b) << endl;
Top* p = static_cast<Top*>(&b);
cout << *p << endl;
cout << static_cast<void*>(p) << endl;
cout << dynamic_cast<void*>(p) << endl; return 0;
} ///:~
输出结果:
28
1,2,3,4
0043FABC
1
0043FAD0
0043FABC
对象大小与实现有关,28 = 4 * 4 + 2 * 4 + 1 * 4(大概是4个int,2个ptr to virtual base,1 个vptr)
注意Bottom里初始化Top的方式,Left、Right给出的对应参数会被编译器巧妙地忽略
内容源自:《TICPP-2nd-ed-Vol-two》
virtual base classes的更多相关文章
- vptr, vtable, virtual base class table
#include <iostream> using namespace std; class X { int x, y, z; }; class Y: public virtual X { ...
- cannot convert from pointer to base class 'QObject' to pointer to derived class 'subClass' via virtual base 'baseClass'
QT 编译不过的另一个问题: 1. 新建一个console工程 QT -= gui CONFIG += c++ console CONFIG -= app_bundle # The following ...
- [C++] OOP - Virtual Functions and Abstract Base Classes
Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...
- python 用abc模块构建抽象基类Abstract Base Classes
见代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/08/01 16:58 from abc import ABCMet ...
- 《深度探索C++对象模型(Inside The C++ Object Model )》学习笔记
转载:http://dsqiu.iteye.com/blog/1669614 第一章 关于对象 使用class封装之后的布局成本: class并没有增加成本,data members直接内含在每一个c ...
- Effective C++ -----条款40:明智而审慎地使用多重继承
多重继承比单一继承复杂.它可能导致新的歧义性,以及对virtual继承的需要. virtual继承会增加大小.速度.初始化(及赋值)复杂度等等成本.如果virtual base classes不带任何 ...
- c语言实现面向对象OOC
这种问题比较锻炼思维,同时考察c和c++的掌握程度.如果你遇到过类似问题,此题意义自不必说.如果用c实现c++,主要解决如何实现封装,继承和多态三大问题,本文分两块说. 1.封装 // Example ...
- C++的黑科技
周二面了腾讯,之前只投了TST内推,貌似就是TST面试了 其中有一个问题,"如何产生一个不能被继承的类",这道题我反反复复只想到,将父类的构造函数私有,让子类不能调用,最后归结出一 ...
- Core Java Volume I — 1.2. The Java "White Paper" Buzzwords
1.2. The Java "White Paper" BuzzwordsThe authors of Java have written an influential White ...
随机推荐
- Unity Animation.CrossFade Animation.Play
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerCo ...
- URAL 1145—— Rope in the Labyrinth——————【求树的直径】
Rope in the Labyrinth Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- iOS 状态栏设置为白色
1.首先需要再info.plist中添加一项View controller-based status bar appearance为no 2.在需要的地方添加代码 [[UIApplication sh ...
- 【杜鹃沙盒】Cuckoo SandBox学习笔记
这是个github上开源前十的项目之一,笔者只完成学习了部分功能,前来分享点经验 整个工程 连接地址 :https://github.com/cuckoosandbox/cuckoo 0x01调试运行 ...
- elasticsearch结构化查询过滤语句-----4
1.之前三节讲述的都是索引结构及内容填充的部分,既然添加了数据那我们的目的无非就是增产改查crudp,我先来讲讲查询-----结构化查询 我们看上图截图两种方式: 1)第一种,在索引index5类型s ...
- 微信小程序电商实战-商品详情(上)
先看一下今天要实现的小程序商品详情页吧! 商品详情.gif 本期我们要实现小程序商品详情页的头部标题.头部轮播.商品详情浮动按钮和商品内页布局. 一.设置头部标题 如上图所示,头部标题是商品详情 ...
- While, DoWhile in WorkFlow.
On 03/03/2010, in 4.0, WF, by bcakiroglu While Activity In a While activity, the activity in the Bod ...
- HBuilder中改造console.info
HBuilder的js中console.info只会输出头一个参数,与谷歌浏览器行为不符合.让人很不习惯. 于是,对其改造一番. window.console.print=window.console ...
- VirtualBox中linux虚拟机和主机间的共享文件设置
设置共享文件路径 点击虚拟机 设置-->选择 共享文件夹 (图1 设置共享文件夹) 设置共享文件夹路径 1 选择路径 2 填写自定义的共享名称(在后面需要与挂载路径相对应) 3 设置自动挂载/固 ...
- DVB数字电视常见信号指标解释
1. 平均功率与峰值电平 峰值电平在模拟电视广播时用于表征频道信号电平强弱. 模拟电视信号是单极性.不对称的,即电视信号有一个固定黑色参考电平,比黑色亮的信号处在黑色电平线一边,同步脉冲处 ...