Why can I access private variables in copy constructor?

I found out that I can never access a private variable, only with the get function in the class. But why can I access it in the copy constructor?

Example:

Field::Field(const Field& f) { pFirst = new T[f.capacity()]; pLast = pFirst + (f.pLast - f.pFirst); pEnd = pFirst + (f.pEnd - f.pFirst); std::copy(f.pFirst, f.pLast, pFirst); } 

My expression:

 private: T *pFirst,*pLast,*pEnd; 
+63
c ++ private access-specifier
Nov 07 '10 at 8:30
source share
5 answers

IMHO, the existing answers give a bad job explaining the "Why" of this - too much attention, repeating what kind of behavior really is. "Access modifiers work at the class level, not at the object level." - yes, but why?

The general concept here is that the programmer (s) designs, writes, and maintains a class that (as expected) understands the desired OO encapsulation and is authorized to coordinate its implementation. So, if you write class X , you code not only how a single object X x can be used by code with access to it, but also like:

  • derived classes can interact with it (via optionally pure virtual functions and / or secure access) and
  • Various X objects collaborate to provide the intended behavior, observing the applied conditions and invariants from your design.

This is not just a copy constructor - a large number of operations can include two or more instances of your class: if you compare, add / multiply / divide, copy, clone, assign, etc., this is often the case when you either just have to have access to private and / or protected data in another object, or you want it to allow faster or faster implementation of a function.

In particular, these operations can use privileged access to do things like:

  • (copy constructors) use the private member of the "rhs" object (right side) in the initialization list, so the member variable itself is built by copying instead of the default one (if it is even legal), and then assigned too (again, if it legally)
  • share resources - files, shared memory segments, shared_ptr to reference data, etc.
  • take responsibility for things, for example. auto_ptr<> "moves" ownership of an object under development.
  • copy private "cached", calibration or government items necessary to create a new facility in an optimally used state without having to regenerate them from scratch
  • copy / access information / trace information stored in the copied object, which is not otherwise accessible through the public APIs, but can be used by some later exception object or logging (for example, something about the time / circumstances when the "original "copy built instance)
  • make a more efficient copy of some data: for example, objects can have, for example, a unordered_map , but publicly set begin() and end() iterators - with direct access to size() you could reserve capacity for faster copying; worse if they only set at() and insert() and otherwise throw ....
  • copy links back to parent / coordination / control objects that may be unknown or write-only for client code.
+19
Jul 18 '13 at 10:40
source share

Access modifiers work at the class level , not at the level level .

That is, two objects of the same class can access the personal data of others.

Why:

Primarily because of efficiency. It would be unproductive runtime at check time if this == other every time you access other.x , which you need if access modifiers work at the object level.

It is also semantically logical if you think about it in terms of scope: "How important is the part of the code that needs to be considered when changing a private variable?" - You need to consider the code of the entire class, and it is orthogonal to what objects exist at run time.

And this is incredibly convenient when writing copy constructors and assignment operators.

+92
Nov 07 2018-10-10T00:
source share

You can access private members of a class from a class, even to another instance.

+26
Nov 07 2018-10-10T00:
source share

To understand the answer, I would like to remind you of several concepts.

  • No matter how many objects you create, there is only one copy of one function in memory for this class. This means that functions are created only once. However, the variables are separate for each instance of the class.
  • this pointer is passed to each function when called.

Now, due to the this pointer, the function can find the variables of this particular instance. regardless of whether it is public. It may be available inside this function. Now, if we pass a pointer to another object of the same class. using this second pointer, we can access private members.

Hope this answers your question.

+10
May 23 '12 at 13:14
source share

The copy constructor is a member function of the class and, as such, has access to the data elements of the class, even those declared as 'private'.

+6
Nov 07 '10 at 9:44
source share



All Articles