Using Private Data in Copy and Move Constructors

Jan 5, 2018 at 4:32pm
How do I implement t copy and move constructors without providing methods to access private data I want to remain "behind the scenes?"
Jan 5, 2018 at 4:47pm
Since constructors class members they can see every class variable no matter what section of the class the variables are in (private, protected, or public).

Jan 5, 2018 at 4:54pm
So are you saying that I could just define a constructor as:
1
2
3
4
5
6
Object::Object(const Object& copy)
{
	x = copy.x;
	y = copy.y;
	z = copy.z;
}


even if x, y and z are private members of Object

If that's not the case, then you misunderstand what I'm asking for. I want to know how I can access private members of copy without having to provide methods, if possible.
Last edited on Jan 5, 2018 at 4:55pm
Jan 5, 2018 at 5:04pm
That is the case.

Any object of type className can access the private variables of any other object of type className.
Jan 5, 2018 at 5:07pm
Alright then. Can I do the same with overloaded assignment operators?
Jan 5, 2018 at 5:07pm
Any class function you like.
Jan 5, 2018 at 5:12pm
Alrighty then. This has been eye-opening since this kinda breaks the rules of encapsulation, but then again it seems to be a rule that C++ just ignores elements of OOP that are too hard to implement.
Jan 5, 2018 at 5:28pm
See also Java and C#, which behave similarly.
Jan 5, 2018 at 5:48pm
Remember you're passing that parameter by a non-const reference.


This has been eye-opening since this kinda breaks the rules of encapsulation,

Some would argue that member functions of any kind break some of the rules of encapsulation, no matter what language you're using.

Jan 5, 2018 at 7:55pm
I don't see how this breaks encapsulation; you're the one designing the intricacies of the class. You would have to know what's internally in the class to make any sense of a constructor, whether a "normal" constructor or a copy constructor.

Does this break encapsulation?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Point {
  public:
    Point(double x, double y)
    {
        this->radius_squared = x*x + y*y;
        this->theta = atan2(y, x);
    }
    Point(const Point& other)
    {
        this->radius_squared = other.radius_squared;
        this->theta = other.theta;
    }
    double getX() {
        return sqrt(radius_squared) * cos(theta);
    }
    double getY() {
        return sqrt(radius_squared) * sin(theta);
    }
  private:
    double radius_squared;
    double theta;
};

I wouldn't say so. You, in order to make the constructor, have to know that internally it's the radius squared being stored. What makes the 2-arg constructor any better than the copy constructor, according to this logic? This isn't the most realistic or efficient example... but I think it shows the basic concept. The user doesn't know what the internal representation of the x and y are, and the user doesn't need to know that. But you, as the class designer, have to either way.

The user of the class still doesn't have to know about the inner details.
1
2
3
4
5
6
7
8
int main()
{
    Point point(3, 2); // normal constructor
    Point origin(0, 0);

    Point p3(point); // encapsulation is not being broken here anymore than above
    cout << p3.getX() << endl;
}


btw, point classes are sometimes simple enough such that the class designers just keep the x and y member variables public, such as in SFML. If the class is simple enough and is meant to just "hold data" instead of being a functional, independent object, then this can be okay.
Last edited on Jan 5, 2018 at 8:14pm
Topic archived. No new replies allowed.