CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    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)

  2. #2
    Join Date
    Apr 2005
    Posts
    62

    Re: just a little stuck....

    Could you be more specific? Why can't you add the object to a list?

  3. #3
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Thumbs up 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)

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: just a little stuck....

    Quote 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?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    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)

  6. #6
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    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)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    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
  •  





Click Here to Expand Forum to Full Width

Featured