文字是对象吗? [英] Are literals objects?

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

问题描述

你好。


我想知道字面常量是否是对象,或者它们只是

" naked"没有包含在任何对象中的值?

我已经读过编译器可能不会分配文字常量

。如果标准说对象是

a存储区域,我从中推断出文字常量

不是对象,因为它们可能不会被归为存储在内存中的区域



如果字面常量

是对象或者标准,则标准本身不会告诉任何内容不是[至少我没找到任何东西]。它只是说文字常量是表达式。


我的想法是否正确?


我是问这个是因为有人告诉我,文字

常数是对象,下面的代码证明了这一点:


const int& x = 7;

std :: cout<< x;


他说它是一个对象,因为它可以设置一个

常量引用。谁在这里?


-

SasQ

Hello.

I wonder if literal constants are objects, or they''re only
"naked" values not contained in any object?
I have read that literal constants may not to be allocated
by the compiler. If the Standard is saying that "object is
a region of storage", I deduce from that that literal constants
aren''t objects because they may not be alocated as regions of
storage in the memory.

The Standard itself doesn''t tell anything if literal constants
are objects or not [at least I haven''t found anything]. It
only says that literal constants are expressions.

Are my thoughts correct?

I''m asking about this because some person told me that literal
constants are objects and that the following code prooves it:

const int& x = 7;
std::cout << x;

He said that it''s an object because it is possible to set a
constant reference to it. Who''s right here?

--
SasQ

推荐答案

On Fri,2007年4月6日01:06:01 +0200,Noone写道:
On Fri, 06 Apr 2007 01:06:01 +0200, Noone wrote:

你好。


我想知道如果文字常量是对象,或者它们只是裸,那么
值是否包含在任何对象中?我已经读过,文字常量

可能不会由编译器分配。如果标准说

对象是存储区域,我从中推断出文字

常量不是对象,因为它们可能没有被分配作为内存中的
存储区域。
Hello.

I wonder if literal constants are objects, or they''re only "naked"
values not contained in any object? I have read that literal constants
may not to be allocated by the compiler. If the Standard is saying that
"object is a region of storage", I deduce from that that literal
constants aren''t objects because they may not be alocated as regions of
storage in the memory.



我不认为文字常量是对象。对或错,我认为对象必须具有属性和展示行为:字段和

方法。文字常量只有一个字段值,因此除非在特定的上下文中使用,否则没有

的含义。

I do not consider literal constants to be objects. Right or wrong, I
believe an object must have attributes and exhibit behavior: fields and
methods. A literal constant only has a field value and therefore has no
meaning unless it is used in a particular context.


SasQ写道:
SasQ wrote:

你好。


我想知道文字常量是否是对象,或者它们只是

" naked"没有包含在任何对象中的值?

我已经读过编译器可能不会分配文字常量

。如果标准说对象是

a存储区域,我从中推断出文字常量

不是对象,因为它们可能不会被归为存储在内存中的区域



如果字面常量

是对象或者标准,则标准本身不会告诉任何内容不是[至少我没找到任何东西]。它只是说文字常量是表达式。


我的想法是否正确?
Hello.

I wonder if literal constants are objects, or they''re only
"naked" values not contained in any object?
I have read that literal constants may not to be allocated
by the compiler. If the Standard is saying that "object is
a region of storage", I deduce from that that literal constants
aren''t objects because they may not be alocated as regions of
storage in the memory.

The Standard itself doesn''t tell anything if literal constants
are objects or not [at least I haven''t found anything]. It
only says that literal constants are expressions.

Are my thoughts correct?



你为什么关心?

Why do you care ?


>

我''问这个是因为有人告诉我文字

常数是对象,以下代码证明了这一点:


const int& x = 7;
>
I''m asking about this because some person told me that literal
constants are objects and that the following code prooves it:

const int& x = 7;



这表示从文字7创建临时int并绑定对x的引用。

That says create a temporary int from a literal 7 and bind a reference to x.


std :: cout<< X;
std::cout << x;



将x的值传递给operator<<。


在这种情况下,编译器可以自由分配没有存储

无论是x还是它所指向的int。我会下注

,有些编译器会认为代码完全相同。


std :: cout<< int(7);

Pass the value of x to operator<<.

In this case, the compiler is quite free to allocate no storage
whatsoever for x or the int it is pointing to. I would make a wager
that some compilers would treat that code exactly the same as it would.

std::cout << int(7);


>

他说它是一个对象,因为它可以设置一个

常量引用它。谁在这里?
>
He said that it''s an object because it is possible to set a
constant reference to it. Who''s right here?



const引用允许编译器为

创建任何类型的临时对象,可以从文字7构造。 br />

ie


struct A

{

A(int);

};


const A& a = 1;


这是如何证明的什么,我们为什么要关心?

A const reference allows the compiler to create a temporary object for
any kind of type that can be constructed from a literal 7.

i.e.

struct A
{
A( int );
};

const A & a = 1;

How does this "prove" anything and why would we care ?


SasQ写道:
SasQ wrote:

你好。


我想知道字面常量是否是对象,或者它们只是

" naked"任何对象中都没有包含的值?
Hello.

I wonder if literal constants are objects, or they''re only
"naked" values not contained in any object?



如果他们是对象,他们会有一个地址。


-

伊恩柯林斯。

If they were objects, they would have an address.

--
Ian Collins.


这篇关于文字是对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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