Get/Set Problems using reference and const...

Hello all - fairly new to C++ - my second college class on it and I am usually great about being able to google-foo my way thru things I get stuck on but this has me stumped.

Basically we have some already completed code. We have to write the class definition and implementation - so it boils down to creating some methods for get/set for some x/y cordinates - I have wrote the SetX() SetY() and Set() as well as a GetX() GetY() and Get()methods - The SetX and SetY methods work fine - The GetX and GetY methods do as well.

I'm not so sure about the Set() or Get() methods... I believe the Set() method is working but the Get() method isnt -

Set call: (Pre-code can't be changed)
P4.Set(13.5, 16.2);

Set Definition:
void Set(double point1, double point2);

Set Method:
1
2
3
4
void Point::Set(double point1, double point2)
{ x = point1;
  y = point2;
} //Set() 



Here's where I am lost - the homework says Get() must be declared const - it also says that to obtain the coordinates of a point, we must use reference parameters for the Get() method.

This is where I have become stumped I am just not that good with reference, pointers, and const. I have wrote and rewrote the Get() method many times without success - everything works up until I call the Get() method - the returning result is always the last value that I set for x/y using the SetX() and SetY() command.

Get call: (Pre-code can't be changed)
P4.Get(x,y);

Get Definition:
double Get(double &point1, double &point2)const;

Get Method:
1
2
3
4
5
double Point::Get(double &point1, double &point2) const
{ const double &x = point1;
  const double &y = point2;
  return x,y;
}




My Get() function is probably totally screwed up right now - I have spent probably 3 to 4+ hours coding, recoding, googling, and now it's some montrosity that I dont even recognize or understand and I am lost as to how to fix it. I've get the general idea of reference and pointers but I clearly dont understand the correct usage when it comes to passing them between methods back to main. If anyone can look at this and help me understand this I would be greatful - even a corrected get method that works so I at least have a working example to draw from would be super.

I read somewhere that a method can't return more than one value - so is that why you have to use references? By me trying to return x,y is that even working? Needless to say I am a bit confused...
The purpose of the Get function is to pass in two parameters and have the value of those parameters changed. This prototype should make it obvious what to do.

void Point::Get(double &someValueThatWillBeChangedToHaveTheValueOfX, double &someValueThatWillBeChangedToHaveTheValueOfY);


In your Get function above, you seem to be trying to change the value of x and y. Why would you do that? The Get function is for reading those values; it's the set function that was for changing them.
Last edited on
Honestly, probably because I had tried everything else and was lost...

So I have this now...

Get Method:
1
2
double Point::Get(double &point1, double &point2) const
{ return point1, point2;}


I assume that was what you were getting at correct? Unfortunatly that isnt working either =(

Same result in that X and Y's values havent changed... thats why I thought the problem might be in the Set() method - but I cout'd the values within the method and they appear to be fine there.
I assume that was what you were getting at correct?


No.

See how my prototype has a void at the front? That means it doesn't return anything.

The clue is in the variable names. I'll do a bit more:
1
2
3
4
5
void Point::Get(double &someValueThatWillBeChangedToHaveTheValueOfX, double &someValueThatWillBeChangedToHaveTheValueOfY)
{
  someValueThatWillBeChangedToHaveTheValueOfX = x;
  // Now you work out the rest
}
I assume that was what you were getting at correct?

No. See how my prototype has void for the return type? That means it doesn't return anything.

This: return point1, point2; does not return two objects. You cannot return more than one object from a function. You are passing in the parameters by reference. This means that anything that happens to them inside the function stays happened when the function ends. Here's a simple example:

1
2
3
4
5
6
7
8
9
10
11
// define a function
void aSimpleFunction(int & parameter)
{
  parameter = 8; // change the parameter to 8
}


int x = 7;
cout << x; // What will this be? It will be 7
aSimpleFunction(x);
cout << x; // What will this be? It will be 8. 

You need to read up on passing by reference


The clue was in the variable names. I'll write a bit more.
1
2
3
4
5
void Point::Get(double &someValueThatWillBeChangedToHaveTheValueOfX, double &someValueThatWillBeChangedToHaveTheValueOfY)
{
  someValueThatWillBeChangedToHaveTheValueOfX = x;
  // now you do the rest...
}
Last edited on
Well I got it - boy do I feel stupid....

1
2
3
4
void Point::Get(double &point1, double &point2) const
{ point1 = x;
  point2 = y;
}


Working like a charm now...
One last thing... I initially had this method like this before I changed it all to hell and back -

1
2
3
4
void Point::Get(double &point1, double &point2) const
{ x = point1;
  y = point2;
} 



So I was thinking in reverse I guess... Thanks a lot I appreciate it.
Last edited on
Topic archived. No new replies allowed.