Copy Constructor that would be invoked wasn't invoked


Hello, I am a beginner in C++, and I have been struggling with the following codes regarding of the copy constructor.


The following codes are inefficient in that this code:

MyInteger SecondInteger(FirstInteger.Return(FirstInteger));

will invoke the copy constructor for two times.


First time is that the function FirstInteger.Return(FirstInteger) will return an object by value, thus invoking the copy constructor. Second time is that we use an object to directly initialise another object which is "SecondInteger".

I understand that the following code requires a move constructor to become more efficient. BUT! When I try the following code out, the compiler tells me the following:

Invoke constructor
Invoke copy constructor
Invoke Destructor
Invoke Destructor

As you can see, the copy constructor is only invoked once, and that's what drives me crazy. Is it because I am using the Microsoft Visual Studio Express 2013, and it has some optimisation that behaves like a move constructor? I understand that the following codes are quite long, but can someone help me with this please?

[#include <iostream>

using namespace std;

class MyInteger
{


public:
int* Resource;
MyInteger(int Input)
{
if (Input != NULL)
{
cout << "Invoke constructor" << endl;
Resource = new int;
*Resource = Input;
}
else
Resource = NULL;
}

~MyInteger()
{
if (Resource != NULL)
{
cout << "Invoke destructor" << endl;
delete Resource;
}
}

MyInteger(const MyInteger& Input)
{
if (Input.Resource != NULL)
{
cout << "Invoke copy constructor" << endl;
Resource = new int;
*Resource = *Input.Resource;
}
else
Resource = NULL;
}




MyInteger Return(MyInteger& Input)
{

return Input;
}
};

int main()
{
MyInteger FirstInteger(27);

MyInteger SecondInteger(FirstInteger.Return(FirstInteger));

return 0;
}]

P.S. I understand that it's not a good idea to put the class member: Resource in the public, please just ignore this :)
Please use code tags when posting code.

Your code fails to compile on my machine because of the following:
1
2
3
        MyInteger(int Input)
        {
            if(Input != NULL)

main.cpp|13|error: NULL used in arithmetic [-Werror=pointer-arith]|
First if your compiler supports C++11 you should be using nullptr instead of NULL. Second you shouldn't be trying to compare an int to NULL (or nullptr). NULL (or nullptr) should only be used in connection to pointers.

Why do you think the copy constructor should be called more than once?

Last edited on
Thanks for your reply, jlb.

I think it will be called more than once because of this code:

MyInteger SecondInteger(FirstInteger.Return(FirstInteger));

The 'FirstInteger.Return(FirstInteger)' will return an object by value, thus invoking a copy constructor.

The 'MyInteger SecondInteger(An object returned by the above function )' will also invoke a copy constructor since we are using an object to initialise another object.

Please correct me if I am wrong.
What contructors, if any, are invoked when passing objects by value depends on both how the object came to be and what its ultimate fate is.

The rules are summarized at http://en.cppreference.com/w/cpp/language/copy_elision

In your case, compilers are expected (and as of C++17 required) to only call the copy constructor once in your code. That copy constructor creates main::SecondInteger using Return::Input (which aliases main::FirstInteger) as its argument.
Last edited on
Topic archived. No new replies allowed.