Click to See Complete Forum and Search --> : just a little stuck....
Vaderman
July 22nd, 2005, 08:48 AM
I have the following code:
myObject = new CMyClass();
myObject->MyMethod()->CreateInstance("fgdffgdg")
where MyMethod returns a singleton object. But what I would like to know is how do I add the object to a list before returning back?
CMyClass
{
CSingleton* m_singletonClass;
CSingleton* MyMethod()
{
// here I'd like to add to a list after its been created, and then return
return m_singletonClass;
}
};
Any ideas?
Regards
John
naddad
July 22nd, 2005, 08:52 AM
Could you be more specific? Why can't you add the object to a list?
Vaderman
July 22nd, 2005, 08:57 AM
ooops... sorry, just solved it!
Regards
John
cilu
July 22nd, 2005, 08:58 AM
where MyMethod returns a singleton object. But what I would like to know is how do I add the object to a list before returning back?
John/Darth Vaderman, you really put me in darkness with this question. What list are you talking about?
Vaderman
July 22nd, 2005, 09:01 AM
Apologies Cilu. I was basically trying to do too much (just trying to be flash with my code) and it wasn't working...
Just solved it and made it more simplier.. :)
Regards
John
Vaderman
July 22nd, 2005, 11:47 AM
erm, one more question - which I suppose is not related but, hey ho here goes...
I have a class, and I've limited the number of instances on a class, similar to the design pattarn of a singleton and each instance created returns a new instance of the class on the heap as shown :
CDevice* CDevice::CreateInstance()
{
CDevice* ptrDevice = NULL;
if (m_nCounter < m_nMaxObjects)
{
ptrDevice = new CDevice;
ptrDevice->m_bDeviceStatus = FALSE;
ptrDevice->m_nDeviceType = DEVICE_RADIATOR;
ptrDevice->m_strDescription ="";
m_nCounter++;
}
return ptrDevice;
}
and to create an instance:
m_pDevice = CDevice::CreateInstance();
where m_pDevice is declared as a CDevice*.
Now, I've saved these instances in a vector as follows :
m_pvecDevice.push_back(m_pDevice);
This is where I'm having difficulties: How do I release the memory for each of these instances?
I attempt to release the memory as follows in the dtor() of another class:
while (it != m_pvecDevice.end())
{
m_pDevice = (CDevice*)*it;
// m_pDevice->Release(); // Results in stack overflow
delete m_pDevice; // can't do this - cannot access private member
it++;
}
In my Device Class, I have a Release() method, but it seems that it can't delete itself :
void CDevice::Release()
{
if (this != NULL)
{
delete this;
m_nCounter--;
}
}
as this produces a stack overflow - but i can't imagine why. :confused: Initially, I ensured that the m_nCounter flag was > 0 before deleting and then decrementing.
Can anyone pointer me in the right direction? :D
Regards
John
Paul McKenzie
July 22nd, 2005, 12:03 PM
I attempt to release the memory as follows in the dtor() of another class:Create a member function of CDevice that deletes the instances -- don't do this directly from another class. Make the other class call the CDevice member to delete the instance(s).
Not only will this work, but you now no longer write classes that needs to know how those objects were allocated in CDevice. It's CDevice that allocated the instances, CDevice should be the only one that knows how to destroy the instances.
Regards,
Paul McKenzie
Paul McKenzie
July 22nd, 2005, 12:12 PM
Also, I feel you are overcomplicating the deletion of the instances with Release, delete this, etc.
c
Just create a simple statiCDevice function, call it RemoveInstance(), that takes a pointer to an instance. All the CDevice::RemoveInstance() does is call delete on the passed-in pointer. Simple.
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.