#include<iostream>
#include<map>
#include<vector>
#include<string>
using namespace std;

class ObjTrace {
public:
  string tag;
  int n;

  ObjTrace(string tag, int n) {
    this->tag = tag;
    this->n = n;
    cout<<tag<<"("<<n<<"): i live"<<endl;
  }
  ObjTrace(const ObjTrace& other) {
    this->tag = other.tag;
    this->n = other.n+1;
    cout<<tag<<"("<<n<<"): a new me"<<endl;
  }
  ~ObjTrace() {
    cout<<tag<<"("<<n<<"): i die"<<endl;
  }
};

class Bar:ObjTrace{
public:
  Bar(string tag):ObjTrace(tag, 0) {
  }
};

class Foo:ObjTrace{
public:
  vector<Bar> stuff;
  Foo(string tag):ObjTrace(tag, 0) {
  }
};

int main() {
  map<string, Foo> foo;
  foo.insert(make_pair("a", Foo("a")));
  vector<Bar> *vec = &foo.find("a")->second.stuff;
  vec->push_back(Bar("b"));
  vec->push_back(Bar("c"));
  cout<<"cleanup."<<endl;
  foo.clear();
  cout<<"end."<<endl;
}

/*
? g++ foo.cpp -o foo
? ./foo
a(0): i live
a(1): a new me
a(2): a new me
a(3): a new me
a(4): a new me
a(3): i die
a(2): i die
a(1): i die
a(0): i die
b(0): i live
b(1): a new me
b(0): i die
c(0): i live
c(1): a new me
b(2): a new me
b(1): i die
c(0): i die
cleanup.
c(1): i die
b(2): i die
a(4): i die
end.
? g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
*/