为什么从std :: map读取不被视为const? [英] Why does reading from a std::map not considered const?

查看:63
本文介绍了为什么从std :: map读取不被视为const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么读取std :: map的成员不被视为const?例如:


class My_Class

{

int Get_Map_Value(int index)const // **错误**未考虑

const !!!

{

返回m_Map [index]; //注意m_Map没有改变,只读

来自

}

std :: map< int,int> m_Map;

};


如果尝试将const用于其预期目的,这真的是一团糟......


[== Peteroid ==]


PS - 我正在使用MS VC ++ .NET并创建一个托管C ++应用程序,如果那样

事情......

解决方案

Peteroid写道:

为什么要阅读std的成员::地图不算const?例如:

类My_Class
{get /Map_Value(int index)const // **错误**不考虑
const !!!
{
返回m_Map [index]; //注意m_Map没有改变,只读
}
std :: map< int,int> m_Map;
};

这真的是在试图将const用于其预期目的时肆虐......

[== Peteroid ==]

PS - 我正在使用MS VC ++ .NET并创建一个托管C ++应用程序,如果这很重要......




如果地图中没有该关键字,map :: operator []将创建一个

新(K,V)对并将其存储在地图中,通过引用返回V部分。

(使用此运算符要求mapped_type是默认构造的。)


您可以通过使用map :: find来避免此行为,尽管您的函数如上面指定的
要么必须返回默认值,要么抛出

异常,如果该函数返回end()。


-

Doug Harrison

Microsoft MVP - Visual C ++


所以,如果我说得对,那就用std :: map上的operator []不是const,因为

它可能给出的可能性钥匙还没有出现在地图上。

因此,即使我的代码确保我也不会要求输入一个钥匙

还不存在,它不能被声明为const因为我可能不会在这方面写好

好​​的代码。


我相信我现在明白了,但觉得它应该一直是这样的情况,

operator []被设计为允许const并且如果

请求没有密钥存在的条目,则只会导致错误,并且迫使人们使用count()方法检查是否存在(如果条目

不存在则返回0,如果条目确实存在,则返回1)。就是这样对待它就像试图
访问一个超出界限的数组元素...


谢谢道格!


[== Peteroid ==]


" Doug Harrison [MVP]" < ds*@mvps.org>在消息中写道

news:cr ******************************** @ 4ax.com ...

Peteroid写道:

为什么读取std :: map的成员不被视为const?对于
示例:
类My_Class
{get /Map_Value(int index)const // **错误**不考虑
const !!!
{
返回m_Map [index]; //注意m_Map没有改变,只读
}
std :: map< int,int> m_Map;
};

这真的是在试图将const用于其预期的
目的时肆虐?
[== Peteroid ==]

PS - 我正在使用MS VC ++ .NET并创建一个托管C ++应用程序,如果
那么...



如果密钥是'n'地图中已经存在,map :: operator []将创建一个新的(K,V)对并将其存储在地图中,通过引用返回V部分。
(使用此运算符要求mapped_type是默认构造的。)

你可以通过使用map :: find来避免这种行为,尽管你上面指定的函数要么必须返回默认值,要么抛出如果该函数返回end(),则会出现异常。

-
Doug Harrison
Microsoft MVP - Visual C ++



Peteroid写道:


所以,如果我说得对,在std :: map上使用operator []不是const,因为
它可能给予的可能性给予一张尚未出现在地图中的钥匙。
因此,即使我的代码确保我也不会要求输入一个钥匙
尚未存在的条目,它可以'因为我可能不会在这方面写出好的代码而被声明为const。

我相信我现在明白了,但感觉应该是
operator []的情况被设计为允许const并且如果对没有密钥存在的条目的请求请求只是导致错误,并强制人们使用count()方法检查是否存在(返回0条目
不存在,如果条目存在,则为1。就是这样,就像试图访问一个超出范围的数组元素一样......




const是C ++中的堕胎。 />

从理论上讲,这听起来是个好主意。在实践中,它只是不起作用。


Why does reading a member of a std::map not considered const? For example:

class My_Class
{
int Get_Map_Value( int index ) const // ** error ** not considered
const!!!
{
return m_Map[index] ; // note that m_Map is not changed, only read
from
}
std::map<int,int> m_Map ;
} ;

This really reeks havoc when to try to use const for its intended purpose...

[==Peteroid==]

PS - I''m using MS VC++.NET and creating a Managed C++ application, if that
matters...

解决方案

Peteroid wrote:

Why does reading a member of a std::map not considered const? For example:

class My_Class
{
int Get_Map_Value( int index ) const // ** error ** not considered
const!!!
{
return m_Map[index] ; // note that m_Map is not changed, only read
from
}
std::map<int,int> m_Map ;
} ;

This really reeks havoc when to try to use const for its intended purpose...

[==Peteroid==]

PS - I''m using MS VC++.NET and creating a Managed C++ application, if that
matters...



If the key isn''t already present in the map, map::operator[] will create a
new (K,V) pair and store it in the map, returning the V part by reference.
(Use of this operator requires the mapped_type be default-constructible.)

You can avoid this behavior by using map::find, though your function as
specified above will either have to return a default value or throw an
exception if that function returns end().

--
Doug Harrison
Microsoft MVP - Visual C++


So, if I get you right, using operator[] on a std::map isn''t const because
of the POSSIBILITY it might give be given a key not already in the map.
Thus, even if my code insures I won''t ever ask for an entry for which a key
doesn''t already exist, it can''t be declared const since I might not write
good code in this regard.

I believe I now understand, but feel it should have been the case that
operator[] was designed to allow const and to just cause an error if a
request for an entry where no key existed was asked for, and force people to
check on existence using the count() method (which returns 0 if the entry
doesn''t exist and 1 if it does). That is, treat it much like trying to
access an array element that is out of bounds...

Thanks Doug!

[==Peteroid==]

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:cr********************************@4ax.com...

Peteroid wrote:

Why does reading a member of a std::map not considered const? For example:
class My_Class
{
int Get_Map_Value( int index ) const // ** error ** not considered
const!!!
{
return m_Map[index] ; // note that m_Map is not changed, only read
from
}
std::map<int,int> m_Map ;
} ;

This really reeks havoc when to try to use const for its intended purpose...
[==Peteroid==]

PS - I''m using MS VC++.NET and creating a Managed C++ application, if thatmatters...



If the key isn''t already present in the map, map::operator[] will create a
new (K,V) pair and store it in the map, returning the V part by reference.
(Use of this operator requires the mapped_type be default-constructible.)

You can avoid this behavior by using map::find, though your function as
specified above will either have to return a default value or throw an
exception if that function returns end().

--
Doug Harrison
Microsoft MVP - Visual C++



Peteroid wrote:


So, if I get you right, using operator[] on a std::map isn''t const because
of the POSSIBILITY it might give be given a key not already in the map.
Thus, even if my code insures I won''t ever ask for an entry for which a key
doesn''t already exist, it can''t be declared const since I might not write
good code in this regard.

I believe I now understand, but feel it should have been the case that
operator[] was designed to allow const and to just cause an error if a
request for an entry where no key existed was asked for, and force people to
check on existence using the count() method (which returns 0 if the entry
doesn''t exist and 1 if it does). That is, treat it much like trying to
access an array element that is out of bounds...



const is an abortion in C++.

In theory, it sounds like a good idea. In practice, it just doesn''t work.


这篇关于为什么从std :: map读取不被视为const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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