Returning Null??

P

Patrick

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
 
R

Rolf Magnus

Patrick said:
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.
 
N

Nick Hounsome

Patrick said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top