• Advertisement
Sign in to follow this  

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

This topic is 3229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You don't have a copy c-tor defined, let alone one that outputs to cerr, did you forget to paste that?

Share this post


Link to post
Share on other sites
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 :) )

Share this post


Link to post
Share on other sites
Thanks for explaining Zahlman, but it was a fat brain fart ^^. I shouldn't have posted so quick.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement