I have the following questions about the class described in the code below:
#include <vector>
#include <tuple>
#include <map>
using namespace std;
class Widget
{
public:
Widget( int x, int y, int z ) : m_x{x}, m_y{y}, m_z{z} {}
private:
int m_x, m_y, m_z;
};
class Coord {
public:
Coord( int x, int y, int z ) : p{x}, q{y}, r{z} { }
std::tuple<int,int> get_tuple() { return std::make_tuple(p, q); }
int x() { return p; }
int y() { return q; }
int z() { return r; }
private:
int p, q, r;
};
class Collector
{
public:
Collector(const std::vector<Coord>& pool) { collect(pool); };
void collect(const std::vector<Coord>& pool)
{
for ( auto v : pool )
{
tuple<int,int> key = v.get_tuple();
auto value = make_shared<Widget>(v.x(), v.y(), v.z());
m_container.insert(make_pair(key, value));
}
}
private:
map< tuple<int,int>, shared_ptr<Widget> > m_container;
};
int foo()
{
Coord a{0,0,1};
Coord b{0,1,0};
Coord c{1,0,0};
vector<Coord> pool{a, b, c};
auto col = std::make_shared<Collector>( pool );
return 0;
}
I would like to know if it is RAII compliant. If not, how do I modify its implementation to make it so.
I wanted to confirm that the map
Collector::m_container
is stored on the free-store. Are both the tuple-keys and the Widgets both stored on the free-store?Does
foo
leak memory?In what order are the allocated resources destroyed?