ulysse31 Master Cheater
Reputation: 2
Joined: 19 Mar 2015 Posts: 324 Location: Paris
|
Posted: Wed Dec 23, 2015 8:25 pm Post subject: Function that runs post object creation to assign a pointer |
|
|
Hi,
I receive a Packet that initiates a Monster class, I have designed it this way :
I got 20 monster pointers as global variables that I assign to Monster object upon creation, the code is as follows :
Code: | ptrMobKn = boost::shared_ptr <Monsters>(new Monsters(vectArgument, nAnalyzedSize, ptrMobKn)); |
The Monster class constructor is :
Code: | class Monsters
{
public:
std::vector <unsigned char> m_Id;
long long m_llId;
int m_X;
int m_Y;
int m_Z;
bool m_LifeState;
Monsters::Monsters() {
}
Monsters::Monsters(std::vector <unsigned char> &vectArg, int &nAnalyzedSize, boost::shared_ptr < Monsters > & MonsterPtr) : m_LifeState(1)
{
m_X = TurnVectInto32_int(vectArg, 26, nAnalyzedSize);
m_Y = TurnVectInto32_int(vectArg, 30, nAnalyzedSize);
m_Z = TurnVectInto32_int(vectArg, 34, nAnalyzedSize);
for (int uuu = 10; uuu < 18; ++uuu)
{
m_Id.push_back(vectArg[uuu + nAnalyzedSize]);
}
m_llId = TurnVecInto64_int(vectArg, 10, nAnalyzedSize);
MonstersMap[m_llId] = MonsterPtr;
}
void updatePtrValue(boost::shared_ptr <Monsters> Mobptr);
void GetHandle();
Monsters & GetAHandle();
}; |
my map and my function to update it :
Quote: | std::map<long long, boost::shared_ptr <Monsters>> MonstersMap;
void Monsters::updatePtrValue(boost::shared_ptr <Monsters> MonsterPtr) {
MonstersMap[m_llId] = MonsterPtr;
} |
My problem is : the way it's coded I pass the object its own smart pointer and the object stores it into a map within which they key value is the monster ID.
The problem is when I pass the pointer at object creation time it's not yet pointing to the object, it has to wait the return of the object constructor to get assigned to the object therefore when the constructor stores the ptr, its basicly storing an empty pointer. My work around this has been to then use an 'update' ft that just updates the ptr stored into map, i run this function right after constructor.
I am looking for a neater way to do this that doesn't involve me using a function after the constructor IE something that would create a thread that waits constructor to end and then by itself updates the ptr in the map, that thread would be initiated by the constructor. I was thinking this is a callback but after further reading it seems not, I am wondering if there is a simple way to do this such as using pre defined c++ features ?
Thanks !
|
|