C++@子类类型转换为父类类型
static_cast(*this) to a base class create a temporary copy.
class Window { // base class
public:
virtual void onResize() { ... } // base onResize impl
...
};
class SpecialWindow: public Window { // derived class
public:
virtual void onResize() { // derived onResize impl;
static_cast<Window>(*this).onResize(); // cast *this to Window,
// then call its onResize;
// this doesn't work!
... // do SpecialWindow-
} // specific stuff
...
};
Effective C++: What you might not expect is that it does not invoke that function on the current object! Instead, the cast creates a new, temporary copy of the base class part of *this, then invokes onResize on the copy!
*******************************************************************************************
Contrast:
static_cast<Window>(*this)
with:
static_cast<Window&>(*this)
One calls the copy constructor, the other does not.
*******************************************************************************************
Because you are casting actual object not a pointer or reference. It's just the same with casting double to int creates new int - not reusing the part of double.
double类型转换为int型会创建一个新的int型变量?
*******************************************************************************************
上面的句子static_cast<Window>(*this).onResize();千万别改成下面这样,那样会更悲惨!
virtual void onResize()
{
static_cast<Window*>(this)->onResize(); //调用基类的onResize()
derived_ = 2;
std::cout <<"S" << " base_=" << base_ << ",derived=" << derived_ << std::endl;
}
实际上,可能只有C的高手初转C++,对C++对象的内存模型还不很清楚的情况下才会写成这样的代码。
对一个指针,不论我们怎样强制转换,它指向的那段内存的内容并没有改变。
函数把本类对象的指针强制转换为基类对象的指针,只意味着“通过这个指针只能访问基类的成员”了,而对象的内容并没有改变。onResize是虚函数,调用它是先通过对象中指向虚表的指针找到虚表,然后在虚表中找到onResize函数的指针,最后通过函数指针调用函数。 this指针强制转换后,内存没有改变,所以指向虚表的指针没有改变,所以它找到的虚表仍是派生类的虚表,自然找到的函数指针仍是派生类的onResize函数的指针,所以这里就成了一个无穷递归调用,结果就是消耗完栈空间。
将函数修改为这样:你就会看到不断的输出ReCall.
virtual void onResize()
{
std::cout<<"ReCall"<<endl;
static_cast<Window*>(this)->onResize(); //调用基类的onResize()
derived_ = 2;
std::cout <<"S" << " base_=" << base_ << ",derived=" << derived_ << std::endl;
}
C++@子类类型转换为父类类型的更多相关文章
- cocos2d-x getParent() 获得一个父类的一个node型指针,转换为父类类型
void CenterLayer::zhanzheng(CCObject* pSender){ ((GameScene*)this->getParent())->showLayer(Gam ...
- 小问题,小细节要注意(string类型转换为bool类型)
一个表中的推荐字段是bit类型的,添加的时候推荐有两个值,如<asp:RadioButtonList ID="RadioButtonList1" runat="se ...
- c++ string类型转换为char *类型
string 是c++标准库里面其中一个,封装了对字符串的操作 把string转换为char* 有3中方法: 1.data 如: string str="abc"; char *p ...
- 注意SSIS中的DT_NUMERIC类型转换为字符类型(比如DT_WSTR)时,会截断小数点前的0
我们知道SSIS中有许多数据类型,如下图所示: 但是DT_NUMERIC这个类型有个陷进要注意,我们来做个实验,随便定义一个String类型的SSIS包变量,然后打开该变量表达式窗口: 在变量表达式窗 ...
- Java进阶(二十三)java中long类型转换为int类型
java中long类型转换为int类型 由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参 ...
- Qt中实现将float类型转换为QString类型
在使用Qt Creator编程时,难免会用到将float类型转换为QString类型的时候下面是我所有的方法: 1. 将QString类型转化为float类型,很简单 QString data; fl ...
- 深度学习原理与框架-Tensorflow基本操作-mnist数据集的逻辑回归 1.tf.matmul(点乘操作) 2.tf.equal(对应位置是否相等) 3.tf.cast(将布尔类型转换为数值类型) 4.tf.argmax(返回最大值的索引) 5.tf.nn.softmax(计算softmax概率值) 6.tf.train.GradientDescentOptimizer(损失值梯度下降器)
1. tf.matmul(X, w) # 进行点乘操作 参数说明:X,w都表示输入的数据, 2.tf.equal(x, y) # 比较两个数据对应位置的数是否相等,返回值为True,或者False 参 ...
- python中bytes类型转换为str类型
使用的原因:基于URL解析报文的时候,要使用str类型,但是提供的确实bytes类型,报错: TypeError: must be str, not bytes 所以就把bytes类型转换为str类型 ...
- Date类型转换为Integer类型
Date类型转换为Integer类型: Integer date = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMMd ...
随机推荐
- CSS实现图片变灰色及透明度
[图片变灰] 每当遇到哀悼日,很多网站快速变灰色,来看看实现方式吧: 方式一,仅支持ie) html{filter:progid:DXImageTransform.Microsoft.BasicIma ...
- swing LayoutManager 和多态
interface LayoutManager{ void show();}class FlowLayout implements LayoutManager{ public void show(){ ...
- CSS练习一(模仿163邮箱登陆)
// '); code = code.replace(/&/g, '&'); return code; }; var runCode = function (code) { if (c ...
- C++11 move_iterator
template<typename Iterator> class move_iterator { Iterator current; public: typedef Iterator i ...
- type safe printf
在书里看到的,摘录如下: #include <iostream> #include <stdexcept> template<class T> struct is_ ...
- Xtreme Toolkit Pro 免费下载地址
Xtreme Toolkit Pro 是针对Windows程序员的一套先进的用户界面套包,强大的功能可使您的应用程序具有专业的.现代感的外观. Xtreme Toolkit Pro 由8个专业级的构件 ...
- 一次蜿蜒曲折的RFID破解之路
前言 早一段时间看到一篇看雪论坛关于逻辑嗅探破解接触式IC卡口令的文章,激起鄙人对rfid的兴趣.遂准备拿学校的卡一展身手. 0×00 前期准备 经过初步了解,学校的rfid卡片分为两种.校园卡采用M ...
- 《Programming with Objective-C》的常规规定
1.每个类的类名在软件中必须唯一的(包括在框架.bounds中),类名有多个单词的时候就是用驼峰方法. Prefix Framework NS Foundation (OS X and iOS) an ...
- Android AIDL使用详解
1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间 ...
- Newspaper Headline_set(upper_bound)
Description A newspaper is published in Walrusland. Its heading is s1, it consists of lowercase Lati ...