c++classconstructorsmart-pointersstdmap

Getting object with a unique_ptr member from a std::map()


I am trying to get a class object (with a unique_ptr member) from a std::map (previously filled), and error occurs. Here is the code:

classes.hpp

#ifndef CLASSMETHODNOTCALLED_CLASSES_HPP
#define CLASSMETHODNOTCALLED_CLASSES_HPP

#include "iostream"
#include "memory"

// tow classes with one pointing to the other

class Ca
{
    int m_va = 5;
public:
    Ca();
    int getVa();
};


class Cb
{
    std::unique_ptr<Ca> m_x; // a smart ptr pointing to class Ca
    int m_vb;
public:
    explicit Cb(int n);
    ~Cb();
    int getCa();
};
#endif //CLASSMETHODNOTCALLED_CLASSES_HPP

classes.cpp

#include "../include/classes.hpp"
#include "memory"

// Ca
Ca::Ca()
{

}

int Ca::getVa()
{
    return m_va;
};

// Cb
Cb::Cb(int n)
{
    m_x = std::make_unique<Ca>();
    m_vb = n;
}

Cb::~Cb()
{
}

int Cb::getCa()
{
    return m_x->getVa() + m_vb; // returns 5 + 1 = 6 if works properly
}

main.cpp

#include <iostream>
#include "include/classes.hpp"
#include "map"

int main()
{
    Cb cb(1);  // instanciate Cb

    std::map<int, Cb> m;
    m.emplace(1, std::move(cb));

    Cb ccb = m.at(1); // error occurs here complaining about: call to implicitly deleted copy constructor
    int i = ccb.getCa();

    std::cout << "m_va = " << i << std::endl;
}

Error occurs in main.cpp, and compilor complains about "call to implicitly deleted copy constructor" at line

Cb ccb = m.at(1); 

My question is, how to retrieve the cb object from the previously filled std::map() in this case? Thank you!


Solution

  • std::unique_ptr cannot be copied, so Cb's copy constructor is implicitly deleted by the compiler.

    This code:

    Cb ccb = m.at(1);
    

    invokes the deleted copy constructor.

    So, either call getCa without using an intermediate variable:

    m.at(1).getCa()
    

    Or, change your variable to a reference:

    Cb& ccb = m.at(1);
    

    Either way, no Cb object needs to be copied.