first of all i think its a crapy design , but im trying to prove a point.
i want to count all the instances of derivers from my class, im trying to do it like so:
.h file:
#ifndef _Parant
#define _Parant
#include<map>
class Parant
{
public:
Parant();
virtual ~Parant();
static void PrintInstances();
private:
static void AddInstance(const char* typeName);
static std::map<const char*, int> InstanceCounter;
};
#endif
.cpp file:
#include "Parant.h"
#include <typeinfo>
#include <iostream>
using namespace std;
Parant::Parant()
{
AddInstance(typeid(this).raw_name());
}
Parant::~Parant()
{
}
std::map<const char*, int> Parant::InstanceCounter;
void Parant::AddInstance(const char* typeName)
{
InstanceCounter[typeName]++;
}
void Parant::PrintInstances()
{
for(map<const char*,int>::iterator i = InstanceCounter.begin(); i != InstanceCounter.end(); i++)
{
cout << " typename: " << i -> first << " ;;" ;
cout << " count: " << i -> second << endl ;
}
}
i have to inheritors that look like this(the cpp contains empy implemintations):
#pragma once
#include "parant.h"
class ChildA :
public Parant
{
public:
ChildA(void);
virtual ~ChildA(void);
};
and this is the main function:
int main()
{
ChildA a;
ChildB b;
ChildA a1;
Parant::PrintInstances();
....
the result i get is:
typename: .PAVParant@@ ;; count: 3
help, why doesnt it work?
edit: i changed it to AddInstance(typeid(*this).raw_name()); of course it still doesnt work, though now i understand why... can i get it to work?