| Simon L 2006-01-09, 11:10 pm |
| I've got a singleton class with a load a static functions and
variables. It's basically a container for a class of objects
#include <vector>
using namespace std;
class Groups
{
public:
~Groups(void);
static Groups m_Group;
Groups( LPCTSTR lpszName , eTYPE eType , CString strChildren );
static void LoadGroups();
static Groups* FindGroup(LPCTSTR lpszName);
private:
Groups(void);
static vector<Groups*> m_vAllgroups;
};
//in .cpp
Groups Groups::m_Group;
vector<Groups*> Groups::m_Allgroups;
Groups::Groups(void)
{
LoadGroups();
}
Groups::Groups( LPCTSTR lpszName , eTYPE eType , CString strChildren )
{
m_strName = lpszName;
m_eType = eType;
}
void Groups::LoadGroups()
{
//...
Groups* pg = new Groups( strNewGroupName , eType , strChildren );
m_vAllgroups.push_back(pg);
}
Groups* Groups::FindGroup(CString strChild)
{
if(strChild.IsEmpty())
return NULL;
for(vector<Groups*>::iterator i=m_vAllgroups.begin();
i!=m_vAllgroups.end(); i++)
{
Groups* p = *i;
if( strChild == p->GetName() )
{
return p;
}
}
return NULL;
}
Now it seems to work OK unless I declare m_vAllgroups as static, in
which case calls to push_back seem to work OK but when I get to a
function needing an iterator, m_vAllgroups seems empty. Any ideas?
Also, is this a good way to implement a singleton class? It means I can
always access Groups::m_Group or any of the Groups data from anywhere
in my application without having to pass pointers around.
cheers.
|