I'm trying to create a global (singleton) class that can associate any type of object with an integer value. I was thinking of using a map<T*, int> variable, but was wondering if there was any other way to do it.
UPDATE: Here is a prototype of my class. Let me know what I'm doing wrong ! It compiles and work fine, but I'm not sure if it could be done in a better way. For instance, int addr = (int)&objectA; looks pretty ugly.
UPDATE 2: I've chosen to go the inheritance way as proposed by Potatoswatter. That will be much easier considering what I'm trying to achieve. Thanks everyone for the feedback !
#include <iostream>
#include <map>
#include <string>
using namespace std;
struct PortPin {
int port;
int pin;
};
class Carte {
public:
void associate(int object_address, int port, int pins);
map<int, PortPin> map_;
};
void Carte::associate(int object_address, int port, int pins) {
PortPin values = {port, pins};
map_[object_address] = values;
}
class A {
public:
A() {}
};
class B {
public:
B() {}
};
void main() {
Carte carte;
A objectA;
B objectB;
int addr = (int)&objectA;
carte.associate(addr, 2, 7);
cout << "Port: " << carte.map_[addr].port
<< " Pin: " << carte.map_[addr].pin;
}