June 20th, 2003, 06:12 PM
How to return NULL when return type is user defined
Please read this code and the question asked at the end.
int x, y;
Point(int x, int y); //constructor
int getX(); // getter
int getY(); // and
void setX(int x); // setters
void setY(int y); // functions
bool operator==(Point p) const; //overloaded == operator
~Point(); // destructor
Point findPoint(Point p); //what should be the prototype of this function
Point Box::findPoint(Point p)
for(int i=0; i<40; ++i)
pTemp = points[i];
if(pTemp == p )
return; // what to return here ?????????????
Look at the findPoint function. The responsibility of this function is to search in arrary
for the Point given and if function finds this Point, return it, otherwise if function does
not find the Point given it should return NULL. But I am unable to do it. I can achieve this
by changing the prototype of function like this
Point *findPoint(Point p);
butt I dont want to do this because it will break the conecpts of OOP and give access to
private data memeber.
The problem is if I return by value then I cant return NULL, so in user class or function,
I can not know whether the Point is found or not. and if I return the pointer, then I give
direct access to private data member.
Anybody help please !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
June 21st, 2003, 03:02 AM
Well, since the findPoint method appears to test for equivalence, you can just as well return a boolean value, and if true, continue on using the Point you specified as the argument to the method call.
Another approach might be passing the Point argument by-pointer, having the findPoint method set its attributes accordingly, and return a boolean.
And if you don't want -that-, you can always pass -two- Points, one by value, and one by pointer:
Good luck :)
Point pTemp, pResult;
"A poor programmer is he who blames his tools."
June 21st, 2003, 07:00 AM
I tend to agree with Analyser, that all you're after here is an equivalence test. You already have a copy of the Point you're looking for.
As a general case, though, let's imagine you don't have a copy of the Point, but you're searching based on some matching criteria (maybe just a certain value of x, for instance). In that case, rather than returning a pointer to the Point within the Box object that meets the criteria, you could dynamically create a new copy of that Point object and pass back a pointer to that new copy, or return NULL if no match is found. I guess it depends what you want to do with the Point that gets returned.
June 21st, 2003, 06:45 PM
Butt if we dynamically create a new of that Point object, who will destroy that Point object. Is it a good idea to allocate memory in one method and deallocate it another location in the program. Will this not lead to the memory leaks????????
By the way, I changed the prototype of finPoint to
bool findPoint(Point pSearch, Point &rpResult);
It is fulfilling the purpose.
June 21st, 2003, 07:16 PM
 It would have to be up to the client to deallocate the memory.
 Yes, it's a good idea to allocate memory in one method and deallocate at another location in the program, as long as you tell people that they need to deallocate it. It happens all the time.
 No, it won't lead to memory leaks, as long as the client remembers to deallocate the memory. Just as it won't lead to memory leaks if the client allocates memory somewhere and has to remember to deallocate it. There's nothing inevitable about memory leaks with dynamic memory allocation.