 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Patrick Guest
|
Posted: Thu Feb 26, 2004 8:45 pm Post subject: Returning Null?? |
|
|
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
|
Posted: Thu Feb 26, 2004 9:03 pm Post subject: Re: Returning Null?? |
|
|
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
|
Posted: Thu Feb 26, 2004 11:09 pm Post subject: Re: Returning Null?? |
|
|
"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)
|
|
| Back to top |
|
 |
Ney André de Mello Zunino Guest
|
Posted: Fri Feb 27, 2004 12:40 pm Post subject: Re: Returning Null?? |
|
|
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 |
|
 |
|
|
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
|
|