|
-
July 22nd, 2005, 08:48 AM
#1
just a little stuck....
I have the following code:
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?
Code:
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
I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)
-
July 22nd, 2005, 08:52 AM
#2
Re: just a little stuck....
Could you be more specific? Why can't you add the object to a list?
-
July 22nd, 2005, 08:57 AM
#3
Re: just a little stuck.... [Resolved]
ooops... sorry, just solved it!
Regards
John
I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)
-
July 22nd, 2005, 08:58 AM
#4
Re: just a little stuck....
 Originally Posted by Vaderman
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?
-
July 22nd, 2005, 09:01 AM
#5
Re: just a little stuck....
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
I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)
-
July 22nd, 2005, 11:47 AM
#6
Re: just a little stuck....
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 :
Code:
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:
Code:
m_pDevice = CDevice::CreateInstance();
where m_pDevice is declared as a CDevice*.
Now, I've saved these instances in a vector as follows :
Code:
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:
Code:
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 :
Code:
void CDevice::Release()
{
if (this != NULL)
{
delete this;
m_nCounter--;
}
}
as this produces a stack overflow - but i can't imagine why. Initially, I ensured that the m_nCounter flag was > 0 before deleting and then decrementing.
Can anyone pointer me in the right direction? 
Regards
John
I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)
-
July 22nd, 2005, 12:03 PM
#7
Re: just a little stuck....
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
-
July 22nd, 2005, 12:12 PM
#8
Re: just a little stuck....
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|