How to return NULL when return type is user defined
-
Please read this code and the question asked at the end. class Point { private: int x, y; public: Point(); 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 }; class Box { private: Point points[40]; public: Point findPoint(Point p); //what should be the prototype of this function }; Point Box::findPoint(Point p) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if( pTemp == p ) return pTemp; } 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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
Please read this code and the question asked at the end. class Point { private: int x, y; public: Point(); 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 }; class Box { private: Point points[40]; public: Point findPoint(Point p); //what should be the prototype of this function }; Point Box::findPoint(Point p) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if( pTemp == p ) return pTemp; } 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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Why are you returning a 'Point' anyway? :) If it's found, they've already supplied the point in the first place, so the return result doesn't give them anything new except another point that matches the one they supplied. Maybe a 'bool HasPoint( Point point )' method may suit your needs, or you could return an integer index into the array (and -1 if it doesn't exist)? If you really do want to return a point for some reason then how about:
class Box { private: Point points[40]; public: bool FindPoint( Point search, Point &result ); };
n! -
Please read this code and the question asked at the end. class Point { private: int x, y; public: Point(); 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 }; class Box { private: Point points[40]; public: Point findPoint(Point p); //what should be the prototype of this function }; Point Box::findPoint(Point p) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if( pTemp == p ) return pTemp; } 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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You can do several things. First I would change it to:
bool findPoint( Point& ptFound, Point p );
Another option is to keep your original code and set the returned Point to a value that would never occur in practice. eg. -1,-1. Then you could write:IsPointValid( ptFound = findPoint( point ) )
You can and should also make the function const. You can also return a const ptr, which doesn't break your OOP rule. Solution 3. Use const wherever possible. There you - pick 1. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com -
Why are you returning a 'Point' anyway? :) If it's found, they've already supplied the point in the first place, so the return result doesn't give them anything new except another point that matches the one they supplied. Maybe a 'bool HasPoint( Point point )' method may suit your needs, or you could return an integer index into the array (and -1 if it doesn't exist)? If you really do want to return a point for some reason then how about:
class Box { private: Point points[40]; public: bool FindPoint( Point search, Point &result ); };
n!Thanks for the reply and reminding me my mistake. Now consider the situation. what if findPoint function now has this prototype Point Box::findPoint(int x) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if(pTemp.getX() == x) return pTemp; } return; // what to return here. } The purpose of the findPoint function is to find a Point which has its x data member equal to x provided. So, what to return if the if condition not satisfied.
-
Thanks for the reply and reminding me my mistake. Now consider the situation. what if findPoint function now has this prototype Point Box::findPoint(int x) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if(pTemp.getX() == x) return pTemp; } return; // what to return here. } The purpose of the findPoint function is to find a Point which has its x data member equal to x provided. So, what to return if the if condition not satisfied.
Ahh ok, then I'd pass in a reference to be filled in and return a bool.
bool Box::FindPoint( int x, Point &result ) const { for ( int i = 0; i < 40; ++i ) { if ( points[i].GetX() == x ) { result = points[i]; return true; } } return false; }
n! -
Ahh ok, then I'd pass in a reference to be filled in and return a bool.
bool Box::FindPoint( int x, Point &result ) const { for ( int i = 0; i < 40; ++i ) { if ( points[i].GetX() == x ) { result = points[i]; return true; } } return false; }
n!Can we not use Java style here like this this code fragment is of JAVA language Point findPoint(int x); so, in this case we can return null (cause null is a keyword in java) and also user defined types are reference types in java. My question now is isnt there any alternative in C++ to this JAVA style other than provided by you???
-
Can we not use Java style here like this this code fragment is of JAVA language Point findPoint(int x); so, in this case we can return null (cause null is a keyword in java) and also user defined types are reference types in java. My question now is isnt there any alternative in C++ to this JAVA style other than provided by you???
All objects in Java are reference types, you're not returning by reference you're returning by value here. C++'s class model is different to Java, the closest built in primitive to a Java reference (which may be null, as you said) is a pointer (C++ references may not legally be null), which you said you'd like to avoid. The alternatives I can think of at the moment are: Throw an exception, of course, but that's a bit heavy handed for a failed search :) Return an integer index (with -1 meaning not found) and provide an index operator. Write a RefResult templated wrapper and return one of those, but that doesn't give you anything more than you'd get returning a pointer (except more code). My previous suggestion (returning bool). I can only say that C++ isn't Java and if you prefer the Java style then use Java? :) n!
-
All objects in Java are reference types, you're not returning by reference you're returning by value here. C++'s class model is different to Java, the closest built in primitive to a Java reference (which may be null, as you said) is a pointer (C++ references may not legally be null), which you said you'd like to avoid. The alternatives I can think of at the moment are: Throw an exception, of course, but that's a bit heavy handed for a failed search :) Return an integer index (with -1 meaning not found) and provide an index operator. Write a RefResult templated wrapper and return one of those, but that doesn't give you anything more than you'd get returning a pointer (except more code). My previous suggestion (returning bool). I can only say that C++ isn't Java and if you prefer the Java style then use Java? :) n!
Thank you very much "n!" I have decided to use your earliest suggestion (return bool). Your replies were really very much informative to me. I was really confusing Java and C++ memory management. This confusion is lowered very much by your replies. Thank you.