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++@子类类型转换为父类类型的更多相关文章

  1. cocos2d-x getParent() 获得一个父类的一个node型指针,转换为父类类型

    void CenterLayer::zhanzheng(CCObject* pSender){ ((GameScene*)this->getParent())->showLayer(Gam ...

  2. 小问题,小细节要注意(string类型转换为bool类型)

    一个表中的推荐字段是bit类型的,添加的时候推荐有两个值,如<asp:RadioButtonList ID="RadioButtonList1" runat="se ...

  3. c++ string类型转换为char *类型

    string 是c++标准库里面其中一个,封装了对字符串的操作 把string转换为char* 有3中方法: 1.data 如: string str="abc"; char *p ...

  4. 注意SSIS中的DT_NUMERIC类型转换为字符类型(比如DT_WSTR)时,会截断小数点前的0

    我们知道SSIS中有许多数据类型,如下图所示: 但是DT_NUMERIC这个类型有个陷进要注意,我们来做个实验,随便定义一个String类型的SSIS包变量,然后打开该变量表达式窗口: 在变量表达式窗 ...

  5. Java进阶(二十三)java中long类型转换为int类型

    java中long类型转换为int类型 由int类型转换为long类型是向上转换,可以直接进行隐式转换,但由long类型转换为int类型是向下转换,可能会出现数据溢出情况: 主要以下几种转换方法,供参 ...

  6. Qt中实现将float类型转换为QString类型

    在使用Qt Creator编程时,难免会用到将float类型转换为QString类型的时候下面是我所有的方法: 1. 将QString类型转化为float类型,很简单 QString data; fl ...

  7. 深度学习原理与框架-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 参 ...

  8. python中bytes类型转换为str类型

    使用的原因:基于URL解析报文的时候,要使用str类型,但是提供的确实bytes类型,报错: TypeError: must be str, not bytes 所以就把bytes类型转换为str类型 ...

  9. Date类型转换为Integer类型

    Date类型转换为Integer类型: Integer date = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMMd ...

随机推荐

  1. 推荐一款好用轻便的在线UML画图工具

    刚接触UML时间不长,看了N多教学视频,下载好了几个软件各种不习惯 当我遇见了ProcessOn  从此我彻底“爱上”了它! http://www.processon.com/ UML各类例图它几乎全 ...

  2. a Makefile

    obj-m += showpid.o obj-m += ps.o all: make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) mo ...

  3. Altium Designer 2013 13 复制出错的问题

    刚换成Altium Designer 2013 13,谁知先碰了钉子,为了赶进度需要复制以前的一个原理图上的部分电路图,一复制尽然报错不能复制,通过百度和向高人求助,总结一下两种方法: 1.在电脑上虚 ...

  4. Python的平凡之路(1)

    2016-07-26   一.Python简介   Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.它的特点如下: 面向对象.解释语言.交互性.模块化.动态性.高级语言.可移植.可 ...

  5. Chapter 1: A Simple Web Server

    这算是一篇读书笔记,留着以后复习看看. Web Server又称为Http Server,因为它使用HTTP协议和客户端(一般是各种各样的浏览器)进行通信. 什么是HTTP协议呢? HTTP协议是基于 ...

  6. BZOJ 1045 糖果传递

    奇怪的式子.最后发现取中位数. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  7. CSS样式选择器

    <!-- css样式选择器 HTML选择器 类选择器 ID选择器 关联选择器 组合选择器 伪元素选择器 selector{ /* selector是样式选择器 property:value; / ...

  8. Saving structured data with json

    Strings can easily be written to and read from a file. Numbers take a bit more effort, since the rea ...

  9. SQL基础2

    create database fuxi --创建一个名为“fuxi”的数据库go                   --连接语句use fuxi   --使用名为“fuxi”的数据库gocreat ...

  10. 【转】关于iPhone界面适配详细版本

    对于上面哪一张适配图很多人不了解什么意思,现在我就慢慢地解释一下. 下面我们观看一下我们需要的几张效果图 3GS手机 iPhone 4/4S iPhone 5/5c/5s iPhone 6 iPhon ...