C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Returning Null??

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Patrick
Guest





PostPosted: Thu Feb 26, 2004 8:45 pm    Post subject: Returning Null?? Reply with quote



Using Java I can create the following class method

public ClassA getObject()
{ ClassA result = null;
if (!vectorOfClassAObjects.isEmpty())
{ result = (ClassA)VectorOfClassAObjects.lastElement();
}
return result;
}

Where
ClassA, is a Class
vectorOfClassAObjects, is a vector of ClassA objects

and the method getObject returns the last Object in the vector, or
null if the vector is empty.

I would like to achieve the same behaviour in C++, so far I have

const ClassA& SomeClass::getObject()
{ if (!VectorOfClassAObjects.empty())
{ return VectorOfClassAObjects.back();
}
return ????;
}

What should i return if the vector is empty, is there an equivalent
NULL?


any help appreciated,

pat
Back to top
Rolf Magnus
Guest





PostPosted: Thu Feb 26, 2004 9:03 pm    Post subject: Re: Returning Null?? Reply with quote



Patrick wrote:

Quote:
Using Java I can create the following class method

public ClassA getObject()
{ ClassA result = null;
if (!vectorOfClassAObjects.isEmpty())
{ result = (ClassA)VectorOfClassAObjects.lastElement();
}
return result;
}

Where
ClassA, is a Class
vectorOfClassAObjects, is a vector of ClassA objects

and the method getObject returns the last Object in the vector, or
null if the vector is empty.

I would like to achieve the same behaviour in C++, so far I have

const ClassA& SomeClass::getObject()
{ if (!VectorOfClassAObjects.empty())
{ return VectorOfClassAObjects.back();
}
return ????;
}

What should i return if the vector is empty, is there an equivalent
NULL?

There is no null reference. If you want something like that, use
pointers instead of references. Alternatively, you could throw an
exception if the vector is empty.


Back to top
Nick Hounsome
Guest





PostPosted: Thu Feb 26, 2004 11:09 pm    Post subject: Re: Returning Null?? Reply with quote




"Patrick" <googleaddress (AT) yahoo (DOT) co.uk> wrote

Quote:
Using Java I can create the following class method

public ClassA getObject()
{ ClassA result = null;
if (!vectorOfClassAObjects.isEmpty())
{ result = (ClassA)VectorOfClassAObjects.lastElement();
}
return result;
}

Where
ClassA, is a Class
vectorOfClassAObjects, is a vector of ClassA objects

and the method getObject returns the last Object in the vector, or
null if the vector is empty.

I would like to achieve the same behaviour in C++, so far I have

const ClassA& SomeClass::getObject()
{ if (!VectorOfClassAObjects.empty())
{ return VectorOfClassAObjects.back();
}
return ????;
}

What should i return if the vector is empty, is there an equivalent
NULL?


any help appreciated,


The closest that you can get to java is to use pointers everywhere.

It is an extremely bad idea to try to implement java classes in C++ -
Java classes are the way that they are because it has all the runtime stuff
to protect you.
This doesn't exist in C++ and you will just end up with some ugly code that
leaks memory
and crashes.
About the only java concept that should be brought to C++ is the extensive
use
of interfaces (=classes containing only pure virtual methods)

Quote:
pat



Back to top
Ney André de Mello Zunino
Guest





PostPosted: Fri Feb 27, 2004 12:40 pm    Post subject: Re: Returning Null?? Reply with quote

Patrick wrote:

[...]

Quote:
What should i return if the vector is empty, is there an equivalent
NULL?

If the expected and normal behavior of your getObject() function is to
return a reference to an object, I would think throwing an exception
when there are no objects to be returned would be quite appropriate. The
calling code will have to test the returned value in any case.

Regards,

--
Ney André de Mello Zunino

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.