Node:Setting and Assigning to Points, Previous:Declaring and Initializing Points, Up:Points



Setting and Assigning to Points

It is possible to change the value of the coordinates of Points by using the assignment operator = (Point::operator=()) or the function Point::set() (with appropriate arguments):

     Point pt0(2, 3.3, 7);
     Point pt1;
     pt1 = pt0;
     pt0.set(34, 99, 107.5);
     pt0.show("pt0:");
     -| pt0: (34, 99, 107.5)
     pt1.show("pt1:");
     -| pt1: (2, 3.3, 7)
     

In this example, pt0 is initialized with the coordinates (2, 3.3, 7), and pt1 with the coordinates (0, 0, 0). pt1 = pt0 causes pt1 to have the same coordinates as pt0, then the coordinates of pt0 are changed to (34, 99, 107.5). This doesn't affect pt1, whose coordinates remain (2, 3.3, 7).

Another way of declaring and initializing Points is by using the copy constructor:

     Point pt0(1, 3.5, 19);
     Point pt1(pt0);
     Point pt2 = pt0;
     Point pt3;
     pt3 = pt0;
     

In this example, pt1 and pt2 are both declared and initialized using the copy constructor; Point pt2 = pt0 does not invoke the assignment operator. pt3, on the other hand, is declared using the default constructor, and not initialized. In the following line, pt3 = pt0 does invoke the assignment operator, thus resetting the coordinate values of pt3 to those of pt0.