之前一直理解的构造函数的参数里没有this指针,因为此时对象还没生成,其他非static成员函数才有this指针,因为当它们被调用时对象已经生成了。然而这样理解是错误的,构造函数参数里也是有this指针的,但这个this指针只是指向了一个内存地址,这个内存地址还并不代表一个类对象,当构造函数完成后,这个地址有足够的信息后才表示这是一个类对象。

#include <stdio.h>
#include <stdlib.h>
#include <string>struct Test
{char a;int b;int c;std::string str;
};class CBase
{
public:CBase(): mValue(100){printf("constructor, mValue = %d\n", mValue); }~CBase(){printf("destructor\n"); }int getValue()const{return mValue;}
private:int mValue;Test mData;
};int main()
{printf("sizeof(CBase) = %lu\n", sizeof(CBase));CBase *instance = new CBase();int value = instance->getValue();delete instance;return 0;
}

在 new 那行添加断点,如:

从这里应该可以看出,在构造函数之前,this指针已经分配了,指向的内存为0x602010,那我们可以看下这个内存里有什么,如:

此时内存里都是0,这里的x/8xw 表示打印8个单元内容,每个单元4个字节,以16进制显示,因为sizeof(CBase) = 32,所以这里打印了 32 个字节的内容。再单步往下执行,会调用 struct Test结构体的构造函数,及进行到初始化列表,如:

到这里已经进到构造函数里面了,其成员数据已经初始化完成,我们可以看到此时this指向的内存的内容发生了变化,this指向的前4个字节变成了 0x00000064,其10进制就是:100,就是初始化列表里的 mValue(100),最后两个应该是std::string 变量里的值(std::string 的内存结构这里不详述,有兴趣的同学可以去研究一下)。然后非static成员函数调用时,此时this指针指向的内容就是构造函数构造出来的内容,如:

所以,我们可以得出,构造函数里的this指针指向的就是一块空内存(内容可能也是随机的),而非static成员函数里的this指针,其内存里已经有了初始化值(通过初始化列表或构造函数里赋值)。我们这里是用到了 new 操作符,那我们可以看一下 new 操作符是怎样工作的,这样也可以验证我们的结论。这里找到的文档为 microsoft 的文档:new 操作符如何工作,截取一段内容:

How new works

The new-expression (the expression containing the new operator) does three things:

  • Locates and reserves storage for the object or objects to be allocated. When this stage is complete, the correct amount of storage is allocated, but it's not yet an object.

  • Initializes the object(s). Once initialization is complete, enough information is present for the allocated storage to be an object.

  • Returns a pointer to the object(s) of a pointer type derived from new-type-id or type-id. The program uses this pointer to access the newly allocated object.

The new operator invokes the function operator new. For arrays of any type, and for objects that aren't classstruct, or union types, a global function, ::operator new, is called to allocate storage. Class-type objects can define their own operator new static member function on a per-class basis.

When the compiler encounters the new operator to allocate an object of type T, it issues a call to T::operator new( sizeof(T) ) or, if no user-defined operator new is defined, ::operator new( sizeof(T) ). It's how the new operator can allocate the correct amount of memory for the object.

文档里说明,第一步完成时,此时所分配的内存并未表示是一个 “对象”,当第2步完成后,此时的内存才表示是一个“对象”。

C++ 构造函数与this指针相关推荐

  1. 【C++】C++类的学习(二)——构造函数、析构函数、拷贝构造函数以及this指针

    [fishing-pan:https://blog.csdn.net/u013921430转载请注明出处] 1. 前言 在之前的博文< C++类的学习(一)--初识类>中,简单地讲述了类的 ...

  2. C++小品:吃火锅与shared_ptr,指针,拷贝构造函数和delete

    C++小品:吃火锅与shared_ptr,指针,拷贝构造函数和delete 读者Terry问到一个关于拷贝构造函数的问题,大家可以参考答Terry:拷贝构造函数,其中论述了拷贝构造函数的必要性,然而, ...

  3. 【Smart_Point】C/C++ 中智能指针

    C++11智能指针 目录 C++11智能指针 1.1 C++11智能指针介绍 1.2 为什么要使用智能指针 1.2.1 auto_ptr(C++98的方案,C++11已经抛弃)采用所有权模式. 1.2 ...

  4. 【C++】智能指针(一)入门

    1. 智能指针背后的设计思想 智能指针背后的思想是RAII,参见博客[C++]零散知识 我们先来看一个简单的例子: void remodel(std::string & str) {std:: ...

  5. C++继承中构造函数、析构函数调用顺序及虚析构函数

    首先说说构造函数,大家都知道构造函数里就可以调用成员变量,而继承中子类是把基类的成员变成自己的成员,那么也就是说子类在构造函数里就可以调用基类的成员了,这就说明创建子类的时候必须先调用基类的构造函数, ...

  6. C++智能指针简单剖析

    导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...

  7. Boost智能指针——shared_ptr

    boost::scoped_ptr虽然简单易用,但它不能共享所有权的特性却大大限制了其使用范围,而boost::shared_ptr可以解决这一局限.顾名思义,boost::shared_ptr是可以 ...

  8. C++继承中析构函数 构造函数的调用顺序以及虚析构函数

    首先说说构造函数.大家都知道构造函数里就能够调用成员变量,而继承中子类是把基类的成员变成自己的成员,那么也就是说子类在构造函数里就能够调用基类的成员了,这就说明创建子类的时候必须先调用基类的构造函数, ...

  9. C++——拷贝构造函数

    拷贝构造函数 拷贝构造函数是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象.拷贝构造函数通常用于: 通过使用另一个同类型的对象来初始化新创建的对象. 复制对象把它 ...

最新文章

  1. Android学习笔记之AndroidManifest.xml文件解析
  2. linux环境变量管理器,运维 - linux(ubuntu) 环境变量管理 (持续更新)
  3. 代码体积减少80%!Taro H5转换与优化升级
  4. 网站优化用户体验须面面俱到!
  5. 深度学习笔记 第四门课 卷积神经网络 第二周 深度卷积网络:实例探究
  6. zabbix_fetion_alter
  7. .NetCore中IdentityServer使用nginx-proxy的一次排错经历
  8. error 1307 (HY000):Failed to create procedure
  9. python变量分类_Python 入门系列 —— 5. 三大变量类型介绍
  10. OpenGL基础28:模型
  11. tableau 日周月筛选器_【数据可视化】Tableau教程(六)日历热力图
  12. 卷积码编码和译码c语言,卷积码编码和译码.doc
  13. 【渝粤题库】陕西师范大学201041德育论 作业(专升本)
  14. mysql sysvar int_MySQL:如何编写daemon plugin
  15. 《脱颖而出——成功网店经营之道》一2.2 进货攻略
  16. 设计一个算法求象棋这些棋子各代表哪些数字
  17. 发表说说代码php,PHP随机发送QQ说说[多用户]
  18. ND4J计算特征值和特征向量
  19. 错误nested exception is org.apache.ibatis.binding.BindingException
  20. 广告业务系统 之 敏捷交付 —— “基于 Docker 容器同机部署”

热门文章

  1. linux运维工程师培训课程_《Linux运维工程师必学技能》完整版视频课程专题(1.0)...
  2. 青翼科技自研模块化互联产品 • 模拟采集FMC子卡【产品资料】
  3. ios 实现谷歌地图
  4. Openstack私有云简介之Nova
  5. Hyperledger Fabric 2.x 单机部署多节点网络
  6. Java 进阶 -- 集合(一)
  7. 【Python】09 数值内插
  8. 外贸客户开发的新方式—领英精灵
  9. 关于“过劳死”的法律探讨
  10. VUE 记一次高德地图和Echarts(中国地图)