c++ 构造函数对变量初始化的一种形式实验确认。

对这个数据结构:

struct MemoryBlockRef
{
   void * data;   // the start address
   size_t size;   // the size
  // MemoryBlockRef(){};                                             //Constructor1
   MemoryBlockRef() : data(), size() { }                       //Constructor2
   MemoryBlockRef(void * d, size_t s) : data(d), size(s) { }   //Constructor3
};

----------------------------------------------------------------

//Construct1

MemoryBlockRef ref1;

ref1.data 与 ref1.size 分别是0xcccccccc;

----------------------------------------------------------------

//Construct2

MemoryBlockRef ref2;

ref2.data 与 ref2.size 分别是0;

-----------------------------------------------------------------

验证代码:

#include "stdafx.h"
#include <iostream>

using namespace std;

struct MemoryBlockRef
{
   void * data;   // the start address
   size_t size;   // the size
  // MemoryBlockRef(){};                                       //Constructor1
   MemoryBlockRef() : data(), size() { }                       //Constructor2
   MemoryBlockRef(void * d, size_t s) : data(d), size(s) { }   //Constructor3
};

int main()
{
   char arr[10];
   MemoryBlockRef ref1;
  // MemoryBlockRef ref2;
   MemoryBlockRef ref3((void*) arr,10);

   cout<<"ref1: data=" << ref1.data <<" size ="<<ref1.size <<endl;
   cout<<"ref1 *data=" << &(ref1.data) <<" *size =" << &ref1.size <<endl;

  // cout << "ref2.data=" << ref2.data << " ref2.size = " << ref2.size << endl;
  // cout << "ref2.*data=" << &ref2.data << "&ref2.size = " << &ref2.size << endl;

   cout << "ref3.data=" << ref3.data << " ref3.size = " << ref3.size << endl;
   cout << "ref3.*data=" << &ref3.data << "&ref3.size = " << &ref3.size << endl;


    return 0;
}

验证结果:

1.

ref1.data 与 ref1.size 分别是0xcccccccc;

2.

ref2.data 与 ref2.size 分别是0;

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值