0

I give the following example to illustrate my question:

// Example program
#include <iostream>
#include <string>

class Abc
{
  private:
     int a_;
  public:
     Abc(int a):a_(a) {};
     Abc& operator = ( const Abc &obj);
     Abc(const Abc &obj);
};

Abc& Abc::operator = ( const Abc &obj)
{
   a_ = obj.a_;
   return *this;
}

Abc::Abc(const Abc &obj)
{
    a_ = obj.a_;
}

int main()
{
  Abc obj1(3);
  Abc obj2=obj1;

  return 0;
}

In the above codes, when Abc obj2=obj1 is called, I expect Abc& Abc::operator = ( const Abc &obj) will be invoked while in fact Abc::Abc(const Abc &obj) is called. I am confused about that. On top of it, since a_ is a private member variable, it should not be accessed while a_=obj.a_ seems to work. Any ideas? Thanks.

feelfree
  • 11,175
  • 20
  • 96
  • 167
  • You are not in Java, if the method is protected or private, you can't access it with myobj->my_protected_method(); you are always using ̀this->my_protected_method()`. It's not like java. You have to define a getter to get `a_`. – Pierre Emmanuel Lallemant Feb 02 '16 at 09:47

3 Answers3

7

You need to differentiate initialization and assignment.

Initialization is the, yes, initialization of a variable when it is defined; you set its inital value. Examples:

int i = 2;    // initialization
Abc obj2 = obj1;    // initialization
extern int j;    // this is just a declaration, so no initialization takes place
int j = 2;    // initialization

Assignment is any subsequent... assignment to the variable. Examples:

int i = 2;    // initialization
i = 2;    // ASSIGNMENT

obj2 = obj1;    // assignment

On initialization, the copy constructor is called. On assignment, the assignment operator.


Regarding the private member: the functions manipulating the private member a_ belong to the same class, so they are allowed to access the internals of the object. If this weren't true, moving (>= C++11) and std::unique_ptr would be not possible to implement.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
3
Abc obj2=obj1;

You may think that this would call the assignment operator because it has the = symbol, but this is copy-initialization which will call the copy constructor in this case.

The assignment operator can only be called on objects which have already been constructed. Copy construction is creating an object from another one, copy assignment is assigning an object to an already-created one.

Abc obj2 = obj1; //copy construction
obj2 = obj1;     //copy assignment

Access specifiers apply within the class, not within the object. If a data member is private to class A, any member function of A can access this data member, whether it is their own or it belongs to another instance.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
1

You are still in a class member corresponding to the obj class so even if obj is not the this it can still access to the private members. example:

class Test {
private:
    int a = 42;
public:
    Test(){}
    void change(Test& test) {
        test.a = 21;
    }

};

int main() {
    Test a;
    Test b;
    a.change(b);
    return 0;
}

The C++ protection is at class level not at the instances level. So the matter is that the method you are is in the class not matter of the instance this.