Skip to content

Instantly share code, notes, and snippets.

@komiga
Created July 25, 2011 15:10
Show Gist options
  • Save komiga/1104350 to your computer and use it in GitHub Desktop.
Save komiga/1104350 to your computer and use it in GitHub Desktop.
std::map POD instancing
#include <stdio.h>
#include <map>
class Object;
typedef std::map<int, Object> ObjectMap;
typedef std::map<int, Object*> ObjectPtrMap;
class Object {
public:
Object() {
printf("%s\n", __PRETTY_FUNCTION__);
};
Object(const Object& obj) {
printf("%s\n", __PRETTY_FUNCTION__);
};
Object(int a) {
printf("%s\n", __PRETTY_FUNCTION__);
};
~Object() {
printf("%s\n", __PRETTY_FUNCTION__);
};
};
int main() {
printf("POD:\n");
{
ObjectMap a;
a[1]=Object(1);
a[2];
a.clear();
}
printf("\ndynamic:\n");
{
ObjectPtrMap b;
b[1]=new Object(1);
b[2];
for (ObjectPtrMap::iterator i=b.begin(); i!=b.end(); ++i) {
//printf("k: %d o: %p\n", i->first, (void*)i->second);
if (i->second) {
delete i->second;
}
}
b.clear();
}
return 0;
}
POD:
Object::Object()
Object::Object(const Object&)
Object::Object(const Object&)
Object::~Object()
Object::~Object()
Object::Object(int)
Object::~Object()
Object::Object()
Object::Object(const Object&)
Object::Object(const Object&)
Object::~Object()
Object::~Object()
Object::~Object()
Object::~Object()
dynamic:
Object::Object(int)
Object::~Object()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment