Why is the copy constructor in this code never called?

Hi,

I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
class A
{
    public:
        A(){ cout << "A.ctor()" << endl; }
        // the others methods have similar couts
};

A f() { return A(); }

int main()
{
    f();
}

When I run it, I see:
A.ctor()
A.dector()

which is fine with me.
But when I change the code to:
1
2
3
4
int main()
{
    A a = f();
}

I see the same output o.O
Why is the copy constructor of A never called, and which dector do I call in the end - the one of the temporary object, or the the one of the variable 'a'?
Last edited on
Thanks : )
Topic archived. No new replies allowed.