Skip to main content
gamedev.net
🔒 Locked

Overloading/Return

Started by Tazel 6 replies 200+ views
Original Post
Tazel
Tazel

  
class Distance
{
private:
	int feet;
	float inches;
public:
	Distance operator + ( Distance ) const;
}
Distance Distance::operator + (Distance d2) const
{
	int f = feet + d2.feet;
	float i = inches + d2.inches;
	if(i >= 12.0)
	{
		i -= 12.0;
		f++;
	}
	return Distance(f,i)
}

  
I know how everything works except the return in the overloading. How does the return Distance(f,i) know where to put the returned variables?
tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
jon723
jon723
When overloading an operator you are returning the object that you are performing the operation on. You see how you are manipulating the private variables for your object, i.e. feet and inches. That way your objects provate data can be changed. I hope that clears things up a bit.
www.lefthandinteractive.net
Tazel
Tazel
But how does f,i get into the right places?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Zipster
Zipster
If you have a constructor for distance that takes both feet and inches and puts them into the right variables, then they are stored within the object. When you return a Distance like return Distance(f,i);, it creates a temporary object on the stack. Since you''re not returning a reference, it then copies that temporary into anything that''s being assigned by the operation. For example, if you do:

new_distance = old_distance + another_distance;

Then the temporary object is copied to new_instance using the assignment operator. If you returned a reference, the effect would be the same, however you won''t have to return an entire copy of the object, just a pointer in essence.

way_out_west
way_out_west
When you return a class object by value, depending on wetheral it is assignment or inilialization in the function call (client code), the copy constructor or general constructor of your class is called. Usually you do not need to supply the copy constructor unless your class deals with members on the free store, just as you do not need to supply a constructor in every class definition.

The job of the default copy constructor is to create a clone of the class you are dealing with. First, it sets aside the exactly amount of memory needed for all the types listed in your class and then copies the values of your original class into the new clone.

When your dealing with return types by value, the class'' copy constructor is called (in this case, implicitly) and makes a copy of your object on the stack. The function then returns and execution resumes to the client function. After that, the unnamed variable created on the stack has only 1 opportunity to get used.

For example,


  
Distance totalDistance;
totalDistance = Distance1 + Distance2;


What''s really happening first is this call->

Distance1.operator+(Distance2)

Then, after the function returns with the unnamed class on the stack, this function gets called:

totalDistance(const &rhs UnnamedClassReturnedfromFunction)

(const &rhs UnnamedClassReturnedfromFunction) is the copy constructor that takes the Unnamed class returned from the function by reference and assigns all the new fields to the totalDistance object.)

I hope that helps...
Tazel
Tazel
So do the values get copied into the class from top to bottom? How do you know that i won't go into feet and f go into inches?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)

[edited by - Tazel on January 11, 2003 2:04:25 PM]
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
mond
mond
Read what the Anonymous Poster wrote -- you need a ctor that takes the values in the order you need. That''s how you guarantee the order and that feet goes to feet and inches to inches.

greg

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.