Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2007, 1:19 PM   #11
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 323
Rep Power: 4 Arla is on a distinguished road
Re: Is there a better way to do this?

Quote:
Originally Posted by peaceofpi View Post
Well, I understand what you mean, but you can't return a class instance as a function result.
Really? I'd be REALLY REALLY surprised, if you can't return a struct or a class from a function. If you can't that would seem to be horribly limiting.
Arla is offline   Reply With Quote
Old Nov 25th, 2007, 3:30 PM   #12
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Is there a better way to do this?

Perhaps I worded it wrong. I was incorrect when I said classes couldn't (both can be return types) but structs can't be used with << due to lack of overloading. In your original post you said structures so I took it literally.

edit: It appears I'm wrong again, apparently in C++ you can treat structs exactly like classes. Sorry for the trouble I'm causing!
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.

Last edited by peaceofpi; Nov 25th, 2007 at 3:46 PM.
peaceofpi is offline   Reply With Quote
Old Nov 25th, 2007, 4:08 PM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Is there a better way to do this?

The difference between a struct and a class is default access. This is one of several differences between C and C++ that people forget about when they say C++ is merely a superset of C.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 25th, 2007, 4:50 PM   #14
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Is there a better way to do this?

I still haven't touched on DaWei's suggestion (error checking), but here's the final code.

c++ Syntax (Toggle Plain Text)
  1. struct dms
  2. {
  3. int degrees;
  4. int minutes;
  5. int seconds;
  6. } r;
  7.  
  8. std::ostream& operator<< (std::ostream& os, const dms& r)
  9. {
  10. os << r.degrees << " degrees " << r.minutes << " minutes "
  11. << r.seconds << " seconds";
  12. return os;
  13. }
  14.  
  15. dms deg2dms(double deg)
  16. {
  17. double n = deg;
  18.  
  19. r.degrees = static_cast<int>(n);
  20. n -= r.degrees;
  21. n *= 60.0;
  22.  
  23. r.minutes = static_cast<int>(n);
  24. n -= r.minutes;
  25. n *= 60.0;
  26.  
  27. r.seconds = static_cast<int>(n);
  28. if ( (n - r.seconds) >= 0.5 )
  29. r.seconds += 1;
  30.  
  31. return r;
  32. }
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Nov 25th, 2007, 6:26 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Is there a better way to do this?

Why won't you investigate error checking? You might be perfect, but your users are most assuredly not. Are you a God that that you can command obedience to your wishes without considering stupid responses? I might be wrong, but I think not.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 25th, 2007, 9:35 PM   #16
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 307
Rep Power: 2 Jabo is on a distinguished road
Re: Is there a better way to do this?

Quote:
Originally Posted by DaWei View Post
The difference between a struct and a class is default access.
Another difference to keep in mind is that Classes are byref, structs are byval, when passing to functions; at least in VB, and I'm sure they act the same way in C++
Jabo is offline   Reply With Quote
Old Nov 25th, 2007, 9:41 PM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: Is there a better way to do this?

And how sure are you? Regardless of how sure you are, you are mistaken.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 26th, 2007, 3:32 AM   #18
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,186
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Is there a better way to do this?

Quote:
Originally Posted by Jabo
Another difference to keep in mind is that Classes are byref, structs are byval, when passing to functions; at least in VB, and I'm sure they act the same way in C++
Quote:
Originally Posted by DaWei
And how sure are you? Regardless of how sure you are, you are mistaken.
Dawei is correct. In some languages, like Java, C#, and VB.NET, classes are reference types. This means when you deal with one, the variable that holds it doesn't actually hold it, but instead holds a reference (read: pointer) to it. These objects also exist on the heap, not on the stack, so they persist until the garbage collector reclaims them (which will be an indeterminate time after the last reference goes out of scope). In C# and VB.NET, using a structure is a way around this, and allows you to use a simple value type and avoid the overhead of heap allocation and the garbage collector. For simple data records, a struct is arguably a better thing to use in some situations.

C++ classes, on the other hand, are like any other type in C++ in that they exist on the stack unless you explicitly allocate them on the heap (ie via the new operator, or equivalent).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 26th, 2007, 2:45 PM   #19
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Is there a better way to do this?

DaWei, could you explain a little more about the error checking I should do? I'm open to suggestions, I just don't get what could go wrong (unless they passed a non-number to it).
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Nov 26th, 2007, 3:11 PM   #20
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,186
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Is there a better way to do this?

Quote:
Originally Posted by peaceofpi
DaWei, could you explain a little more about the error checking I should do? I'm open to suggestions, I just don't get what could go wrong (unless they passed a non-number to it).
Well, a parameter of the wrong type would be caught by the compiler, so no worries there.

However, how do you handle conditions such as negative or greater than 360? If you're talking about motion, such as turning, then such conditions are to be expected, and should raise no concerns (though you need to decide which direction a negative measure represents). If you're talking about measurements of angles, such as the corner of a shape, then you're not going to have negatives or angles greater than 360; such parameters should be treated as errors.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC