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 ...
随机推荐
- 随笔1:Markdown语法学习
学习背景 日常工作学习的时候,总喜欢用有道在线笔记记录点东西,不过以往都没太在意笔记的整理和排版,代码或者图片什么的都是直接贴在笔记上,不美观不说,有些代码格式也不容易进行区分,格式也在复制的时候容易 ...
- [转]JavaScriptSerializer中日期序列化
本文转自:http://www.cnblogs.com/songxingzhu/p/3816309.html 直接进入主题: class Student { public int age { get; ...
- Mongodb installation & userguide
1.Mongodb Installation in Ubuntu (1) Download from: https://www.mongodb.org/downloads File: mongodb- ...
- 自己的spring boot starter
这篇文章说的更加详细具体:https://www.cnblogs.com/hjwublog/p/10332042.html 在刚开始看spring boot的时候,发现这么多starter,不免觉得好 ...
- PHP的htmlspecialchars、strip_tags、addslashes解释
第一个函数:strip_tags,去掉 HTML 及 PHP 的标记 注意:本函数可去掉字串中包含的任何 HTML 及 PHP 的标记字串.若是字串的 HTML 及 PHP 标签原来就有错,例如少了大 ...
- http method and status code
http method HEAD: 只返回相应的header POST: 一般用于提交表单 PUT: 向Web服务器上传文件 GET: 查 DELET: 删除 status code 1xx与2xx: ...
- Qt 学习(2)
Qt 学习(2) Qt 的 QXmlStreamReader 在 Qt 应用程序中访问 XML 格式的文件数据,可以使用 [QXmlStreamReader][sreamreader] 对文件进行读取 ...
- db2-表处于暂挂状态
有时当对表数据进行操作时,表锁了,处于暂挂状态,网上搜的大部分不能解决的话可以尝试用以下语句进行解锁 call sysproc.admin_cmd('reorg table 表名')
- 关于th,td,tr的一些相关标签
tr表示行,td表示列,th其实也是表示列但是在这个标签中的文字会以粗体出现 <th>为表格标题,属性summar为摘要, <caption>标签为首部说明, <thea ...
- sqlalchemy使用tip
https://docs.sqlalchemy.org/en/latest/orm/tutorial.html http://docs.sqlalchemy.org/en/latest/core/sq ...