const引用的向量。可能吗? [英] vector of const references. Is it possible?

查看:72
本文介绍了const引用的向量。可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想创建一个const引用的向量。这样的事情:


vector< const x& y;


其中x是类名。


这是一个有效的声明吗?如果我在其他地方使用''y''

的引用会有用吗?


PJ。

Hi,

I wanted to create a vector of const references. Something like this:

vector<const x&y;

where x is a class name.

Is this a valid statement? Will this work if I use a reference of ''y''
somewhere else?

PJ.

推荐答案

ragged_hippy写道:
ragged_hippy wrote:




我想要创建const引用的向量。这样的事情:


vector< const x& y;


其中x是类名。


这是一个有效的声明吗?如果我在其他地方使用''y''

的引用,这会有用吗?
Hi,

I wanted to create a vector of const references. Something like this:

vector<const x&y;

where x is a class name.

Is this a valid statement? Will this work if I use a reference of ''y''
somewhere else?



它不便携。常量和放大器;不可分配,因此不是有效的

容器元素。

It is not portable. const& is not assignable and therefore not a valid
container element.


3月7日,下午12:28,ragged_hippy < pranes ... @ gmail.comwrote:
On Mar 7, 12:28 pm, "ragged_hippy" <pranes...@gmail.comwrote:

我想创建一个const引用向量。这样的事情:


vector< const x& y;


其中x是类名。


这是一个有效的声明吗?如果我在其他地方使用''y''

的引用,这会有用吗?
I wanted to create a vector of const references. Something like this:

vector<const x&y;

where x is a class name.

Is this a valid statement? Will this work if I use a reference of ''y''
somewhere else?



你试过吗?任何编译器都会立即告诉你是否允许指针引用

。 (提示:他们不是,但是矢量需要

指向其容器。)


干杯! --M

Did you try it? Any compiler will instantly tell you whether pointers
to references are allowed. (Hint: they''re not, but vector requires
pointers to its containees.)

Cheers! --M




" ragged_hippy" < pr ******* @ gmail.com写信息

新闻:11 ********************** @ s48g2000cws.googlegr oups.com ...

"ragged_hippy" <pr*******@gmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...




我想创建一个const引用向量。
Hi,

I wanted to create a vector of const references.



为什么?

Why?


这样的事情:


vector< const x& y;


其中x是类名。
Something like this:

vector<const x&y;

where x is a class name.



无效。

Not valid.


>

这是一个有效的声明?
>
Is this a valid statement?



No.

No.


如果我使用''y''$ b的引用,这是否有用$ b
Will this work if I use a reference of ''y''



No.


标准库容器存储对象。参考资料

不是对象。引用是

现有对象的替代名称。

No.

Standard library containers store objects. References
are not objects. References are alternate names for
existing objects.


在其他地方?
somewhere else?



No.


但是,你可以在你的向量中存储指针,因为

指针是对象。


也许如果你描述了你想要解决的问题,我们或许可以给出建议。


-Mike

No.

You can, however, store pointers in your vector, since
pointers are objects.

Perhaps if you describe what problem you''re trying to
solve, we might be able to give advice.

-Mike


这篇关于const引用的向量。可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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