1

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;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Antonio Santoro
  • 827
  • 1
  • 11
  • 29
  • 2
    `&ref` is a `const Integer *`. It's certainly not compatible with the copy constructor. Did you mean to write `std::make_shared(ref);` instead? – François Andrieux Jul 18 '19 at 18:12
  • 1
    Side-note: You don't need to define a copy constructor or a copy assignment operator at all. C++ will make them for you (as well as move equivalent versions, which are not being generated for you because you explicitly defined copy constructor/assignment, though in this case it's mostly harmless since move semantics wouldn't gain you anything). Read up on [the Rule of Zero](https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero) (and the Rule of Three/Five while you're at it); it's key to writing good C++ code. Rule of Zero would nearly halve your lines of code. – ShadowRanger Jul 19 '19 at 14:28

1 Answers1

3

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305