问题描述
我确定已经问了几次,但我仍然不确定。
我想创建一个函数来简化对a的引用CMap在
地图上。
这就是我现在在代码中所做的事情:
std :: map< ; unsigned int,CMap *> :: iterator ThisMapIt = World.Maps.find(
ThisPlayer.Character.Map);
if(ThisMapIt!= World.Maps .end())
{
CMap& ThisMap = *((* ThisMapIt).second);
//使用ThisMap
}
现在,地图数字应该总是在地图上,但我有点迂腐,并且b $ b喜欢检查所有可能的错误。我想创建一个功能来完成
,如:
CMap& FindMap(const unsigned int MapNumber)
{
std :: map< unsigned int,CMap *> :: iterator ThisMapIt = World.Maps.find(
ThisPlayer.Character.Map);
if(ThisMapIt!= World.Maps.end())
return *((* ThisMapIt).second );
其他
//这里有什么回报?引用不能为空!
}
我的替代方法是返回CMap *并在失败时返回NULL,但我会
而不是处理引用,所以我可以使用。而不是 - >
有什么建议吗?
我想我可以返回CMap *并在代码中执行:
CMap * MapP = FindMap(ThisPlayer.Character.Map);
if(MapP!= NULL)
{
CMap&这个地图= * MapP;
//使用这个地图
}
如果我真的需要
抛出异常。
如果函数返回一个引用,它保证如果它返回
,结果就是一个有效的引用。
你可以选择使用(1)对某个特殊对象的引用
表示无对象或失败,或(2)抛出异常。
(2)是最干净,最可靠的。客户端代码检查,可能会帮助
避免构造一个大的虚拟对象。
-
A:因为它搞砸了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门帖子。
Q :usenet和电子邮件中最烦人的事情是什么?
那'是你的选择之一。我可以想到三种方式:
1)返回指针并检查是否为NULL。
2)抛出异常并将其捕获到某处
3)返回对NULL CMap的引用,这是您的
CMap对象的特殊实例,表示不存在地图。
I''m sure this has been asked a few times, but I''m still not sure.
I want to create a function to simplify getting a reference to a CMap in a
map.
This is what I do now in code:
std::map<unsigned int, CMap*>::iterator ThisMapIt = World.Maps.find(
ThisPlayer.Character.Map );
if ( ThisMapIt != World.Maps.end() )
{
CMap& ThisMap = *((*ThisMapIt).second);
// Work with ThisMap
}
Now, the map number should always be in the map, but I''m a bit pedantic and
like to check for all possible errors. I''d like to create a function to do
this like:
CMap& FindMap( const unsigned int MapNumber )
{
std::map<unsigned int, CMap*>::iterator ThisMapIt = World.Maps.find(
ThisPlayer.Character.Map );
if ( ThisMapIt != World.Maps.end() )
return *((*ThisMapIt).second);
else
// What to return here? A reference can''t be null!
}
My alternative is to return a CMap* and return NULL on failure, but I would
rather deal with references so I can use . instead of ->
Any suggestions?
I guess I could return a CMap* and in code do:
CMap* MapP = FindMap( ThisPlayer.Character.Map );
if ( MapP != NULL )
{
CMap& ThisMap = *MapP;
// Work with ThisMap
}
if I really have to
Throw an exception.
If the function returns a reference, it''s guaranteeing that if it
returns, the result is a valid reference.
You have the choice of using (1) a reference to some special object
denoting "no object" or "failure", or (2) throwing an exception.
(2) is most clean, most reliable wrt. client code checking, and may help
avoid constructing a large dummy object.
--
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?
That''s one of your choices. There are three ways that I can think of:
1) Return a pointer and check for NULL.
2) Throw an exception and catch it somewhere
3) Return a reference to a "NULL CMap", that is a special instance of your
CMap object representing that no map exists.
这篇关于什么应该在失败时返回引用返回函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!