// Now can i pass the this stl list to the fun function and access the struct in the function?
Please observe the elements navigation in the Test-class :) :
struct MS {
bool bDataOwner;
int iData;
char* pData;
MS() : bDataOwner(false), iData(0), pData(NULL) {}
~MS() { if (bDataOwner && pData) delete pData ; }
};
typedef std::list<ms*> lstPMS;
typedef lstPMS::iterator iterPMS;
class lstOwnerMS : private lstPMS
{
public:
virtual ~lstOwnerMS() { Empty(); }
void AddData(const MS* pMS) {
lstPMS::push_back(pMS);
}
iterPMS GetHeadPosition() const {
return lstPMS::begin() < lstPMS::end() ? lstPMS::begin() : NULL;
}
MS* GetNext(iterPMS& iter) const {
MS* pResult(NULL);
if (iter < lstPMS::end()) {
pResult = iterPMS;
iterPMS++;
} else {
iterPMS = NULL;
}
return pResult;
}
void Empty() {
while (!lstPMS::empty()) {
delete lstPMS::front();
lstPMS::pop_front();
}
}
};
class IBase
{
public:
virtual void ProcessPMSList(lstOwnerMS& lst) const = 0;
};
class Test : public IBase
{
public:
virtual void ProcessPMSList(lstOwnerMS& lst) const
{
iterPMS iter(lst.GetHeadPosition());
while (iter) {
MS* pMs(lst.GetNext(iter));
if (pMs) {
cout << pMS->iData << " ";
}
}
}
};
int main()
{
lstOwnerMS lstOwner;
lstOwner.AddData(new MS);
MS* pMS(new MS);
pMS->bDataOwner = true;
pMS->pData = new char[2];
strcpy_s(pMS->pData, 2, ".");
lstOwner.AddData(pMS);
Test test;
test.ProcessPMSList(lstOwner);
return 0;
};