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);
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.

Recommended Answers

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

Jump to Post

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 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.