[C++] new does _NOT_ call the constructor!?

Started by
5 comments, last by phresnel 15 years ago
Still working on the same code as my previous post, I noticed something gone wrong (memory leak) and I discovered the following code didn't work like I expected:
class A
{
public:
    A()
    {
        std::cerr << "A();\n";
    }

    ~A()
    {
        std::cerr << "~A();\n";
    }
};

void function(A instance = A())
{
    A *a = new A(instance);
    delete a;
}

int main()
{
    function();
}
I would say, the function accepts an object, passes this to the copy-constructor of A and allocates a new A with it. Not the case. This is what it outputs:
A();
~A();
~A();
However, when you consider the following code:
class A
{
public:
    A()
    {
        std::cerr << "A();\n";
    }

    ~A()
    {
        std::cerr << "~A();\n";
    }
};

void function(A instance = A())
{
    A *a = new A;
    *a = instance;
    delete a;
}

int main()
{
    function();
}
This DOES work like expected, output is:
A();
A();
~A();
~A();
Am I overseeing something? I think this is rather peculiar. Thanks.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
A's copy constructor is getting called in the first one. Since you don't have an output line for the copy constructor you aren't seeing the constructor being called, but it is.
You don't have a copy c-tor defined, let alone one that outputs to cerr, did you forget to paste that?
--Michael Fawcett
'-.-, I'm stupid at times. Thanks lol.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
Still working on the same code as my previous post, I noticed something gone wrong (memory leak) and I discovered the following code didn't work like I expected:

*** Source Snippet Removed ***

I would say, the function accepts an object, passes this to the copy-constructor of A and allocates a new A with it.


More accurately, it default-constructs a default for the parameter (since none is provided), passes that to the copy-constructor of A, and allocates a new A with it.

Quote:Not the case. This is what it outputs:

A();~A();~A();



That doesn't contradict your view at all. You only see one constructor call because the copy constructor doesn't display anything.

Quote:However, when you consider the following code:
*** Source Snippet Removed ***

This DOES work like expected, output is:

A();A();~A();~A();



Because the line "A *a = new A;" calls the default constructor (which does print something), and then "*a = instance;" calls the copy assignment operator.

Quote:Am I overseeing something?


You mean "overlooking", presumably. (Don't worry, I've been speaking English my whole life, and I still hate it :) )
Thanks for explaining Zahlman, but it was a fat brain fart ^^. I shouldn't have posted so quick.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Generally, always keep in mind the C++ Rule of three, you'll get used to it.

Just in case you really want to forbid copy-construction (definitely better than dumping to std::cerr when copy-ctor() is not wanted), you could declare the copy constructor "T(T const&)" and "T& operator = (T const &)" as private (in that case, declaration is enough, no definition needed). Also see the Non-Copyiable Mixin.

This topic is closed to new replies.

Advertisement