0

I have a Points class and a Segment class as follows:

  class Points
  {
     double x; 
     double y;
     int Index=-1;
  }
class Segment
{
  Points Orig;
  Points End;
  Segment(const Points& a, const Points& b){Orig=a;End=b}
  void ChangeGlobalIndex()
  { 
     do something to change the index of two end points.
  }
}
int main()
{
  Points p1(0.0,0.0);
  Points P2(1.0,1.0);
  Segments Line1(p1,p2);
  Line1.ChangeGlobalIndex();
    }
I want to see the Index of p1 and p2 has changed, but it didn't happen. what is the reason?Since the Segment was construced with the references of the two points.
Regards
3
Contributors
2
Replies
14
Views
1 Year
Discussion Span
Last Post by rubberman
0

First your compiler should complain about your Points class and the initialization of index to -1. You need to write the appropriate constructor so that you can properly initialize those private variables. And if you want to access them without getter methods, then you will need to make the member variables public as by default they are private, so your construction code for Points will not work. If you change Points from a class to a structure (a class where unless otherwise specified, everything is public) then your consturction of them will work appropriately.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.