473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 22 '05 #1
3 8617
Patrick wrote:
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.

Jul 22 '05 #2

"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
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)
pat

Jul 22 '05 #3
Patrick wrote:

[...]
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sara | last post by:
We have a Cobol stored procedures on DB2 OS/390. We are able to access the stored procedures from a distributed platform Windows 2000/ Linux / Unix using DB2 Connect. But it does not return column...
0
by: Atul | last post by:
I have a .Net Managed C++ wrapper control for a MFC control. When using the ..Net control in a Visual Studio 2005 Beta 2 C# project and running in Debug mode, I get a crash saying something like...
0
by: George Durzi | last post by:
cross posted in datagrid group. Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be Completed By"> <itemtemplate> <asp:Label...
9
by: Thomas Mlynarczyk | last post by:
Hi, It seems to be a generally adopted convention to have a function return FALSE in case of an error. But if a function is supposed to return a boolean anyway, one cannot distinguish anymore...
1
by: Anup | last post by:
Hi Group, I have a datagrid with dropdown in my application. I want to fill the data in dropdown by an ArrayList for that I am using "e.Item.FindControl("DropdownId")" but this function is...
5
by: anupamjain | last post by:
Tired, Exhausted, searched the web, usenets,forums thorughly but still clueless. I guess it's time to post on the group : This is the issue I have been trying to resolve since today morning : ...
10
by: Sek | last post by:
I have a property that returns a ArrayList object. On failure condition, is it right to return null or return an ArrayList object with zero elements?
3
by: rrpbatni | last post by:
Hi, I have a doubt in C++ where my method setlocale which is passed with two parameters LC_ALL and data_file_locale string for Portuguese locale is failing and returning NULL… What might be the...
1
by: sbowman | last post by:
I have the following CASE statement in a view: (SELECT CASE WHEN ISNULL(.., NULL) = NULL THEN .(..) ELSE .(..) + ', ' + .. END) AS ContactNameAndTitle It works perfectly when...
2
by: aaronharwood | last post by:
I have read dozens of existing responses on threads, that span the last 5 years or so, concerning this problem and while there are many solutions that appear to work for others - none work for me. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.