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

Recommended Answers

All 2 Replies

As long as you don't implement the change, nothing will happen.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.