are copy and move constructors automatic friends?
It is not considered friend, but yes, any member function of class my_str
can access private members of all instances of type my_str
, not just the this
instance:
class my_str {
void foo(my_str& other) {
// can access private members of both this-> and other.
}
static void bar(my_str& other) {
// can access private members of other.
}
};
The general idea behind it is to allow 2 or more objects of the same type to interact without having to expose their private members.
Member functions of the class itself always have access to the private
members, no matter whether the member function is defined in-class or out-of-class and no matter whether it is a special member function such as a copy/move constructor.
Therefore they are not friend
of the class, because that doesn't make any sense. They are already part of the class. Still, they have access to all private
members, not because they are friend
s, but because they are part of the class.
If it wasn't possible to initialized members in a constructor (because they are inaccessible), then the whole concept of member accessibility would be pointless. (How would you initialize the member?)
Also, accessibility is not in any way a matter of the object on which a member is accessed. Accessibility is a matter only of where in the code a name (the name of the member) is used. If a function can access the member of one instance of a class, then it can also access the member of another instance of the same class.