返回STL矢量属性的“只读”视图 [英] Return a 'read only' view of a STL vector attribute

查看:70
本文介绍了返回STL矢量属性的“只读”视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个带有STL向量的类,因为它属性。

如何创建一个只返回''读取的方法 - 只是''看? (即

函数的调用者只能读取向量,而不是写入)?


A类{

私有:

vector< int> _v;

public:

vector< int>& getV(){return _v;} //如何确保调用者只需读取
只读取数组的内容?


}


谢谢。

Hi,

I have a class with a STL vector as it attribute.
How can I create a method which just return a ''read-only'' view? (i.e.
the caller of the function can only read the vector, not write it)?

class A {
private:
vector<int> _v;
public:
vector<int>& getV() { return _v;} // how to make sure the caller can
only read the content of the array?

}

Thank you.

推荐答案

* Al ************ @ gmail.com

我有一个具有STL向量的类作为属性。
如何创建一个只返回只读视图的方法? (即函数的调用者只能读取向量,而不是写入它)?

A类{
私有:
向量< int> _v;
public:
vector< int>& getV(){return _v;} //如何确保调用者只能读取数组的内容?

}

I have a class with a STL vector as it attribute.
How can I create a method which just return a ''read-only'' view? (i.e.
the caller of the function can only read the vector, not write it)?

class A {
private:
vector<int> _v;
public:
vector<int>& getV() { return _v;} // how to make sure the caller can
only read the content of the array?

}




阅读''const''。在_

名称的开头_never_有一个下划线是个好主意。这些名称经常被实现使用。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和e-最令人烦恼的是什么?邮件?



Read up on ''const''. It''s a good idea to _never_ have underscores at the
start of names. Such names are often used by the implementation.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




< Al ************ @ gmail.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...

<Al************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...


我有一个带有STL向量的类,因为它属性。
如何创建一个只返回''只读''视图的方法? (即函数的调用者只能读取向量,而不是写入它)?

A类{
私有:
向量< int> _v;
public:
vector< int>& getV(){return _v;}


此函数不会修改对象的

状态,因此应该是:


vector< int>& getV()const {return _v; }

//如何确保调用者只能读取数组的内容?
Hi,

I have a class with a STL vector as it attribute.
How can I create a method which just return a ''read-only'' view? (i.e.
the caller of the function can only read the vector, not write it)?

class A {
private:
vector<int> _v;
public:
vector<int>& getV() { return _v;}
This function does not modify the object''s
state, so should be:

vector<int>& getV() const { return _v; }
// how to make sure the caller can
only read the content of the array?




返回一个const引用:


const vector< int>& getV()const

{

返回_v;

}


-Mike



Return a const reference:

const vector<int>& getV() const
{
return _v;
}

-Mike


你提到:

这个函数不会修改对象的

状态,所以应该是:


vector< int>& getV()const {return _v; }


但是如果我不把''const'',它仍然可以编译并运行。通过在上面的函数中添加''const',我可以获得什么


You mention:
This function does not modify the object''s
state, so should be:

vector<int>& getV() const { return _v; }

But if i don''t put ''const'', it still compiles and works. What do I gain
by adding ''const'' in the funciton above?


这篇关于返回STL矢量属性的“只读”视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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