It's called a copy constructor
A copy constructor is a member function which initializes an object using another object of the same class.
And a copy constructor can access private members too of the passed object.
for example:
class View
{
private:
int var1, var2;
public:
View(int var1, int var2) { this->var1 = var1; this->var2 = var2; }
// Copy constructor
View(const View &orig) {this->var1 = orig.var1; y = orig.var2; } //Note we are accessing private members of orig
int getvar1() { return var1; }
int getvar2() { return var2; }
};
in main:
int main()
{
View v1(10, 15); // Normal constructor is called here
View v2 = v1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "v1.var1 = " << v1.getvar1() << ", v1.var2 = " << v1.getvar2();
cout << "\nv2.var1 = " << v2.getvar1() << ", v2.var2 = " << v2.getvar2();
return 0;
}
You will see that the members of v1 and v2 have same values assigned.
In most IDEs, copy constructor is "written" in "background" even if you don't write it. That means that you can assign one object to another of the same class type without writing copy constructor in class declaration.