SomeClass(46)
constructs a new instance of SomeClass
on the stack, by calling the constructor passing the number 46
to it.
someClassObject =
some instance of SomeClass
call the operator=
on someClassObject
.
Unless the constructor is declared explicit it could also have been written as:
someClassObject = 46;
Take a look at this example: http://ideone.com/7kgWob pasted below:
#include <iostream>
using namespace std;
class SomeClass
{
private:
int i = 0;
public:
SomeClass() { cout << "default constructor\n"; };
SomeClass(int val) { i = val; cout << "constructor getting int: " << val << '\n'; };
~SomeClass() { cout << "destrucing object having i: " << i << '\n'; };
SomeClass& operator=(const SomeClass& rhs) {
cout << "operator= getting int: " << rhs.i << '\n';
if (this != &rhs) {
i = rhs.i;
}
return *this;
}
};
int main() {
SomeClass a(10);
SomeClass b = SomeClass(20);
SomeClass c(35);
c = SomeClass(46);
return 0;
}
It will output:
constructor getting int: 10
constructor getting int: 20
constructor getting int: 35
constructor getting int: 46
operator= getting int: 46
destrucing object having i: 46
destrucing object having i: 46
destrucing object having i: 20
destrucing object having i: 10
In essence it creates a temporary instance, and sets the current instance to the same value as the temporary (two objects with the same value exists at this point). It then frees the temporary. Since no new
is involved it happens on the stack.