Cant figure out constructor/operator calls

In this code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class A
{
public:
	A(int n = 0) : m_n(n) { }
	A(const A &a) : m_n(a.m_n) { }
	A(A&& rval) : m_n(rval.m_n) { }

	A& operator= (A&& rval) { }
	A& operator= (const A& source) { m_n = source.m_n; return *this; }

	~A() { }
private:
	int m_n;
};

int main()
{
A a = 2;
}


I expected the line A a = 2; to call Ctor(int), then move operator, but actually move doesnt get called and none of the defined operators is called instead. Why is that, how is the temporary assigned to the actual a object?
Last edited on
There is no reason that the move constructor is used since there is only a single object involved.
So are these lines equal ? :
1
2
A a = 2;
A b(2);


It's obvious that second one will call only A(int), but the first one seems like it would also need to call a copy ctor to copy the temporary into a object thats beaing created ?
So are these lines equal ? :
Yes, it wouldn't make sense to create an extra temporary object.

Read this:

http://www.cplusplus.com/doc/tutorial/classes/
"Uniform initialization'
Last edited on
Topic archived. No new replies allowed.