参考问题 [英] References question

查看:90
本文介绍了参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我对参考资料有疑问。考虑以下程序:


class Test

{

int& i;

int & j;

int& k;

};


int main()

{


cout<< 类Test =的大小<< sizeof(类测试)<< endl;

返回0;

}


在我的机器上。这打印12个大小的3个指针。我对

引用的了解是,我们需要在

声明时初始化它们。但是,我们在这里没有这样做。我们也没有定义一个

构造函数。我期待编译错误,说我需要初始化i,j和k。但相反,我有12个!


请解释这个行为。


谢谢,

Anunay

解决方案

Anunay写道:

大家好,

我对参考文献有疑问。考虑以下程序:

类测试
{&/ / i;
int& j;
int& k;
};

int main()

cout<< 类Test =的大小<< sizeof(类Test)<< endl;
返回0;
}

在我的机器上。这打印12个大小的3个指针。我所知道的
参考是,我们需要在
声明时初始化它们。但是,我们在这里没有这样做。我们也没有定义
构造函数。我期待编译错误说我需要初始化i,j和k。但相反,我得到了12个!

请解释这种行为。



sizeof没有实例化一个对象,它只是给出了大小。


但我认为你仍然应该收到错误。哪个编译器?


-

Ian Collins。


Anunay写道:

大家好,

我对参考资料有疑问。考虑以下程序:

类测试
{&/ / i;
int& j;
int& k;
};

int main()

cout<< 类Test =的大小<< sizeof(类Test)<< endl;
返回0;
}

在我的机器上。这打印12个大小的3个指针。我所知道的
参考是,我们需要在
声明时初始化它们。但是,我们在这里没有这样做。我们也没有定义
构造函数。我期待编译错误说我需要初始化i,j和k。但相反,我得到12!




只要你实际上没有创建一个Test类型的对象就有

没有理由错误。仅从

声明中就可以知道对象的大小。




Anunay写道:

大家好,

我对引用有疑问。考虑以下程序:

类测试
{&/ / i;
int& j;
int& k;
};

int main()

cout<< 类Test =的大小<< sizeof(类Test)<< endl;
返回0;
}

在我的机器上。这打印12个大小的3个指针。我所知道的
参考是,我们需要在
声明时初始化它们。但是,我们在这里没有这样做。我们也没有定义
构造函数。我期待编译错误说我需要初始化i,j和k。但相反,我有12个!

请解释这个行为。

谢谢,
Anunay




构造函数

启动之前分配非动态成员的内存...当然,你在这个可爱的

类中有一个默认构造函数。引用也使用内存,比如指针......它们应该已经初始化了

,因为这样它们除了消耗内存之外别无其他。你可以像这样初始化它们:

class测试

{

int& i;

int j;

测试():我(j){}

};


最好的问候


Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!

Kindly explain this behaviour.

Thanks,
Anunay

解决方案

Anunay wrote:

Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!

Kindly explain this behaviour.


sizeof doesn''t instantiate an object, it just gives the size.

But I think you should still get an error. Which compiler?

--
Ian Collins.


Anunay wrote:

Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!



As long as you do not actually create an object of type Test there is
no reason for an error. The size of the object is known from its
declaration alone.



Anunay wrote:

Hello all,

I have a doubt regarding references. Consider the following program:

class Test
{
int &i;
int &j;
int &k;
};

int main()
{

cout<< "size of class Test = " << sizeof(class Test) <<endl;
return 0;
}

On my machine. this prints 12 i.e size of 3 pointers. What i know about
references is that, we need to initialize them at the time of
declaraion. But, here we are not doing so. We are not defining a
constructor also. I was expecting a compilation error saying that I
need to initialize i,j and k. But instead, I got 12!

Kindly explain this behaviour.

Thanks,
Anunay



Memory for non-dynamic members is allocated before constructor
starts... And certainly you have a default constructor in this cute
class. References use memory too, like pointers... They should have
been initialized, because this way they do nothing else but consuming
memory. You coud initialize them like here:
class Test
{
int &i;
int j;
Test():i(j){}
};

Best regards


这篇关于参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆