Michał Suchonos
Michał Suchonos

Reputation: 17

Changing value of variable inside vector doesn't change its value outside of vector

I have a vector of class Point3D objects inside a class Figure3D. Function which changes coordinates of the Point3D object inside a vector, doesn't change coordinates of the Point3D object which is outside the vector.

Using function Figure3D::Pos() I see that the coordinates changed inside the vector after using function Figure3D::Move(), but using Point3D::full_pos() I see that the Point3D object still has its initial coordinates.

#include <vector>
#include <iostream>
#include <math.h>

#define PI acos(-1)

class Point3D {
public:
    Point3D()
    {
        X = 0;
        Y = 0;
        Z = 0;
    }
    Point3D(double a, double b, double c) {
        X = a;
        Y = b;
        Z = c;
    };

    void full_pos() {
        std::cout << "Coordinates of the point are: X = " << X << " Y = " << Y << " Z = " << Z << std::endl;
    }
    void Move(double dx, double dy, double dz) {
        X += dx;
        Y += dy;
        Z += dz;
    }

private:
    double X, Y, Z;
};

class Figure3D :public Point3D {
public:
    Figure3D() {
        f.reserve(10);
    }

    void AddPoint(Point3D *p) {
        f.push_back(*p);
    }

    void Move(double x, double y, double z) {
        for (auto it = f.begin(); it != f.end(); it++) {
            it->Move(x, y, z);
        }
    }
    void Pos() {
        int i = 0;
        for (auto it = f.begin(); it != f.end(); it++) {
            cout << "Position of point " << i << "  X: " << it->posX() << " Y: " << it->posY() << " Z: " << it->posZ() << std::endl;
            i++;
        }
    }
private:
    std::vector<Point3D> f;
};

int main() {
    Point3D p1(1, 2, 3), p2(2, 2, 2), p3(5, 4, 7), p4(4, 9, 0);
    Figure3D f1;

    f1.AddPoint(&p1);
    f1.AddPoint(&p2);
    f1.AddPoint(&p3);
    f1.AddPoint(&p4);

    f1.Pos();
    p1.full_pos();
    f1.Move(10, 10, 10);
    f1.Pos();
    p1.full_pos();

} 

Upvotes: 1

Views: 922

Answers (2)

NutCracker
NutCracker

Reputation: 12253

Your AddPoint function is wrong. You are passing a pointer as an argument but then you are dereferencing a pointer and storing copies of Point3D object into your std::vector. Therefore, it should be:

void AddPoint(Point3D *p) {
    f.push_back(p);
}

instead of

void AddPoint(Point3D *p) {
    f.push_back(*p);
}

and

std::vector<Point3D*> f;

instead of

std::vector<Point3D> f;

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409166

Assuming you expect that the Point3D objects p1 to p4 in the main function to be modified as you modify the elements in the vector in the f1 object, then they wont.

The reson is in the AddPoint function where you do

f.push_back(*p);

The vector stores distinct objects, not pointers or references. This together with your use of the derefernece operator makes you store a copy of the object inside the vector. Modifying a copy will not modify the original.

Upvotes: 4

Related Questions