Search code examples
c++pass-by-referenceshared-ptrcopy-constructormake-shared

Using copy-constructor in conjuction with std::make_shared


I have defined a simple class Integer containing an int value, calling std::make_shared<Integer>(&ref) will force the program to use the constructor accepting an int. Did I implemented a wrong copy-constructor or there's something wrong with my using of std::make_shared? What's the difference of calling std::make_shared<Integer>(ref)? Also, why isn't Integer copied_integer(integer); make use of the copy-constructor? Integer copied_integer(&integer); is not allowed as well.

Here is the code

#include <memory>
#include <iostream>
#include "DoubleLinkedList.h"

class Integer {
private:
    int number;
public:
    Integer(int number) : number(number) {}

    Integer(const Integer &integer) : number(integer.number) {}

    Integer& operator=(const Integer& other) {
        if (this != &other) {
            number = other.number;
        }

        return *this;
    }

    int get() { return number; }

};

int main() {


    Integer integer(30);
    const Integer &ref = integer;
    Integer copied_integer(integer);
    auto pointer = std::make_shared<Integer>(&ref);
    auto sp = std::make_shared<Integer>(10);
    std::cout << "sp -> " << sp->get() << std::endl;
    std::cout << "pointer -> " << pointer->get() << std::endl;
    return 0;
}

Solution

  • Change this:

    auto pointer = std::make_shared<Integer>(&ref);
    

    to this:

    auto pointer = std::make_shared<Integer>(ref);
    

    since you should have received an error similar to:

    gcc-head/include/c++/10.0.0/ext/new_allocator.h:150:20: error: invalid conversion from 'const Integer*' to 'int' [-fpermissive]
      150 |  noexcept(noexcept(::new((void *)__p)
          |                    ^~~~~~~~~~~~~~~~~~
          |                    |
          |                    const Integer*
      151 |        _Up(std::forward<_Args>(__args)...)))
          |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    prog.cc:8:17: note:   initializing argument 1 of 'Integer::Integer(int)'
        8 |     Integer(int number) : number(number) {}
          |             ~~~~^~~~~~
    

    which informs/reminds you that ref is of type const Integer*, and you just need const Integer&, which is the type of the parameter in that copy constructor.