C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 20th, 2003, 05:12 PM
shim shim is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 9 shim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Reply With Quote
  #2  
Old June 21st, 2003, 02:02 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
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:

Code:
Point pTemp, pResult;
findPoint(pTemp, &pResult);


Good luck
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #3  
Old June 21st, 2003, 06:00 AM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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.

Reply With Quote
  #4  
Old June 21st, 2003, 05:45 PM
shim shim is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 9 shim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by BigBadBob

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.


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.

Reply With Quote
  #5  
Old June 21st, 2003, 06:16 PM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Quote:
Originally posted by shim
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????????


[1] It would have to be up to the client to deallocate the memory.

[2] 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.

[3] 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to return NULL when return type is user defined

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap