0

There is a class A with some private data b. If I try to use A.b to get the data, there will be a comppilation error because I'm trying to use the dot-operator on private data.

If I write a copy-constructor for this very class A, I can use the dot-operator to get the private value b from the object I want to copy.

Why?

Gabonica
  • 63
  • 2
  • Duublicate of https://stackoverflow.com/questions/4117002/why-can-i-access-private-variables-in-the-copy-constructor – berkus Oct 01 '21 at 23:49

1 Answers1

-1

If you want to have access to private data you must use set and get methods, because private variables can only be accessed within the same class (an outside class has no access to it). The get method returns the variable value, and the set method sets the value.

Eg, if the instance of the class is called a and the data you want to access is called b, you can use a.getB() or a.setB(value)

Chris Papantonis
  • 720
  • 7
  • 18