函数返回对空对象的引用 [英] function returning reference to empty object
问题描述
亲爱的所有人,
有人可以告诉我如何在错误的情况下返回对空物品的引用
吗?
_not working_我想做的例子:
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回MyClassData();
}
这是编译,但当我没有找到任何合适的数据时,它会在
的情况下返回无效引用。有一种方法如何在这种情况下使用
参考?
我脑子里只有一个解决方案:如果我创建了类变量:
class MyClass {
public:
MyClass():errorVariable(){};
私人:
MyClassData errorVariable;
}
然后我修改功能:
const MyClassData& ; MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回errorVariable;
}
但是我不确定我是否喜欢创建额外的变量
的想法,就像占位符中存在的东西一样。
还有另外一种方法吗?
thx
d。
Dear All,
can someone clarify me how to return the reference to the empty object
in case of error?
_not working_ Example of what i''d like to do:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return MyClassData();
}
This compiles, but of course it returns invalid reference in the case
when I don''t find any suitable data. It there a way how to use the
reference in such case??
I have just one solution in my mind: if I create class variable:
class MyClass {
public:
MyClass () : errorVariable() {};
private:
MyClassData errorVariable;
}
and then I modify function:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return errorVariable;
}
But I''m not sure whether I like the idea of creating additional variable
just as placeholder for something which doesn''t really exist..
Is there another way?
thx
d.
推荐答案
Dejfson写道:
Dejfson wrote:
亲爱的所有人,
有人可以告诉我如何在空错误的情况下返回空白对象的引用
?
Dear All,
can someone clarify me how to return the reference to the empty object
in case of error?
throw。
throw.
_not working_我想做的例子:
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回MyClassData();
}
这是编译,但当我没有找到任何合适的数据时,它会在
的情况下返回无效引用。有一种方法如何在这种情况下使用
参考?
我脑子里只有一个解决方案:如果我创建了类变量:
class MyClass {
public:
MyClass():errorVariable(){};
private:
MyClassData errorVariable;
_not working_ Example of what i''d like to do:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return MyClassData();
}
This compiles, but of course it returns invalid reference in the case
when I don''t find any suitable data. It there a way how to use the
reference in such case??
I have just one solution in my mind: if I create class variable:
class MyClass {
public:
MyClass () : errorVariable() {};
private:
MyClassData errorVariable;
无需使其成为非静态。
No need to make it non-static.
}
然后我修改功能:
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回errorVariable;
}
但是我不确定我是否喜欢创建额外的
变量的想法,就像存在不存在
的东西的占位符一样..
还有另外一种方法吗?
}
and then I modify function:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return errorVariable;
}
But I''m not sure whether I like the idea of creating additional
variable just as placeholder for something which doesn''t really
exist..
Is there another way?
你的班级可以拥有自己的静态实例,例如
class MyClass {
$ b $公开:
...
静态MyClassData errorVariable;
};
.. ..
MyClassData MyClass :: errorVariable;
....
if(数据还可以)
返回数据;
其他
返回errorVariable;
....
if(& returnValueFromFunction ==& errorVariable)
但是''try-catch''和'throw''更好。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复热门回复,请不要问
Your class can have a static instance of itself, something like
class MyClass {
public:
...
static MyClassData errorVariable;
};
....
MyClassData MyClass::errorVariable;
....
if (data are ok)
return data;
else
return errorVariable;
....
if (&returnValueFromFunction == &errorVariable)
But it''s better to ''try-catch'' and ''throw''.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
" Dejfson" < de ***** @ dejfson.org写信息
news:46 ********************* @ newsspool.solnet .ch ..
"Dejfson" <de*****@dejfson.orgwrote in message
news:46*********************@newsspool.solnet.ch.. .
亲爱的所有人,
有人可以告诉我如何在
的情况下返回空对象的引用错误?
_not working_我想做的例子:
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回MyClassData();
}
这是编译,但当然在
我没有找到任何合适的数据的情况下它会返回无效的引用。它有一种方法如何在
这样的情况下使用引用?
我脑子里只有一个解决方案:如果我创建类变量:
class MyClass {
public:
MyClass():errorVariable(){};
私人:
MyClassData errorVariable;
}
然后我修改功能:
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回errorVariable;
}
但是我不确定我是否喜欢创建额外的变量
的想法,只是作为一个不存在的东西的占位符..
是有另一种方式?
Dear All,
can someone clarify me how to return the reference to the empty object in
case of error?
_not working_ Example of what i''d like to do:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return MyClassData();
}
This compiles, but of course it returns invalid reference in the case when
I don''t find any suitable data. It there a way how to use the reference in
such case??
I have just one solution in my mind: if I create class variable:
class MyClass {
public:
MyClass () : errorVariable() {};
private:
MyClassData errorVariable;
}
and then I modify function:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return errorVariable;
}
But I''m not sure whether I like the idea of creating additional variable
just as placeholder for something which doesn''t really exist..
Is there another way?
我这样使用了throw:
CPlayer& FindPlayer(const SOCKET Socket)
{
//在地图中获取该玩家的参考
map_player :: iterator it = World。 ConnectedPlayers.find(Socket);
if(it!= World.ConnectedPlayers.end())
return(* it).second;
else
抛出0;
}
我用多种方式(客户登录后,获取一个参考,
等......)一些使用示例:
//在地图中获取该玩家的参考
试试
{
//只要看看它是否会抛出
FindPlayer(套接字);
}
catch(int)
{
SendMessageToPlayer(Socket,MSG_SERVER_MESSAGE,"< Message
未发送,请重新登录 - 已记录错误>");
World.MessageBuffer.push_back(LogError(未记录客户端
尝试发送消息)
上的socket" + jml :: StrmConvert(Socket)+" IP + IP));
返回0;
}
============= ======
case MSG_REQUEST_PLAYER_INFO://" PlayerID"
{
SOCKET PlayerID = jml :: StrmConvert< SOCKET>(
StrMessage);
尝试
{
CPlayer& TargetPlayer = FindPlayer(PlayerID);
if(ThisPlayer.Character.Map ==
TargetPlayer.Character.Map)//确保在同一张地图上
{
SendMessageToPlayer(套接字,
MSG_CREATE_PLAYER_OBJECT,CreateCharMessage(TargetPlayer.Socket,
TargetPlayer.Character));
}
}
catch(int)
{
}
}
休息;
===================== =
案例MSG_DESTROY_CLIENT:
{
试试
{
CPlayer& ThisPlayer = FindPlayer(Socket);
PlayerLeft(ThisPlayer);
}
catch(int)
{
World.MessageBuffer.push_back(未记入套接字+
jml :: StrmConvert(套接字)+断开连接。);
}
}
休息;
I used throw as thus:
CPlayer& FindPlayer( const SOCKET Socket )
{
// Get a reference in the map for this player
map_player::iterator it = World.ConnectedPlayers.find( Socket );
if ( it != World.ConnectedPlayers.end() )
return (*it).second;
else
throw 0;
}
which I use a number of ways (is a client logged in, get a reference,
etc...) A few examples of usage:
// Get a reference in the map for this player
try
{
// Just do it to see if it throws
FindPlayer( Socket );
}
catch ( int )
{
SendMessageToPlayer( Socket, MSG_SERVER_MESSAGE, "<Message
not sent, please relog - Error has been logged>" );
World.MessageBuffer.push_back( LogError( "Unlogged in client
attempting to send message on socket " + jml::StrmConvert( Socket ) + " on
IP " + IP ) );
return 0;
}
===================
case MSG_REQUEST_PLAYER_INFO: // "PlayerID"
{
SOCKET PlayerID = jml::StrmConvert<SOCKET>(
StrMessage );
try
{
CPlayer& TargetPlayer = FindPlayer( PlayerID );
if ( ThisPlayer.Character.Map ==
TargetPlayer.Character.Map ) // Make sure on same map
{
SendMessageToPlayer( Socket,
MSG_CREATE_PLAYER_OBJECT, CreateCharMessage( TargetPlayer.Socket,
TargetPlayer.Character ) );
}
}
catch ( int )
{
}
}
break;
======================
case MSG_DESTROY_CLIENT:
{
try
{
CPlayer& ThisPlayer = FindPlayer( Socket );
PlayerLeft( ThisPlayer );
}
catch ( int )
{
World.MessageBuffer.push_back( "Unlogged in socket " +
jml::StrmConvert( Socket ) + " disconnected." );
}
}
break;
Dejfson写道:
Dejfson wrote:
亲爱的所有人,
有人可以告诉我如何在空错误的情况下返回对空白对象的引用
? br />
_not working_我想做的例子:
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回MyClassData();
}
这是编译,但当我没有找到任何合适的数据时,它会在
的情况下返回无效引用。有一种方法如何在这种情况下使用
参考?
我脑子里只有一个解决方案:如果我创建了类变量:
class MyClass {
public:
MyClass():errorVariable(){};
私人:
MyClassData errorVariable;
}
然后我修改功能:
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
返回errorVariable;
}
但是我不确定我是否喜欢创建额外的变量
的想法,只是作为一个不存在的东西的占位符..
是还有另一种方式吗?
thx
d。
Dear All,
can someone clarify me how to return the reference to the empty object
in case of error?
_not working_ Example of what i''d like to do:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return MyClassData();
}
This compiles, but of course it returns invalid reference in the case
when I don''t find any suitable data. It there a way how to use the
reference in such case??
I have just one solution in my mind: if I create class variable:
class MyClass {
public:
MyClass () : errorVariable() {};
private:
MyClassData errorVariable;
}
and then I modify function:
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
return errorVariable;
}
But I''m not sure whether I like the idea of creating additional variable
just as placeholder for something which doesn''t really exist..
Is there another way?
thx
d.
在函数内使用静态变量
const MyClassData& MyClass()
{
QListIterator< MyClassDatait(fancylist)
while(it.hasNext())
{
const MyClassData& data = it.next();
if(数据还可以)
返回数据;
}
静态MyClassData errorVariable;
返回errorVariable;
}
这个应该足够安全,因为你要返回一个const引用。
john
Use a static variable inside the function
const MyClassData& MyClass ()
{
QListIterator<MyClassDatait(fancylist)
while (it.hasNext())
{
const MyClassData &data = it.next();
if (data are ok)
return data;
}
static MyClassData errorVariable;
return errorVariable;
}
This should be safe enough since you are returning a const reference.
john
这篇关于函数返回对空对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!