Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Visual-Studio STL
i have an base class like this
 
class Base
    {
    public:
    virtual void fun() const =0;
    };
Derived class like this :
 
  class Derived:public Base 
    {
    virtual void fun()
    {
    //implemtation of fun
    }
    };
 

I had an global structure
 

  struct Mystruct
    {
    int a;
    char *b;
    }MYSTRUCT;
 
I added the structure in list stl as
std::list< MYSTRUCT* > SS1;
     List  = new MYSTRUCT;
        vector<MYSTRUCT*>SS;
    SS.push_back( List);
 
Now can i pass the this stl list to the fun function and access the struct in the function?
Posted 30-Oct-12 10:46am
Comments
Richard MacCutchan at 30-Oct-12 17:01pm
   
You need to declare a version of fun() that takes a parameter of the requested type. Once again I recommend you spending some more time reading your C++ programming manuals.
Sergey Alexandrovich Kryukov at 30-Oct-12 22:17pm
   
Best answer so far. And also, pass the parameter by reference, as a constant reference -- one more topic for reading.
--SA
Richard MacCutchan at 31-Oct-12 4:46am
   
Thanks. Given all the posts from this person, he seems to be trying to learn C++ and develop a complex application at the same time, just by asking questions here.
Sergey Alexandrovich Kryukov at 31-Oct-12 10:44am
   
This is amazing. This person is not the only one. And it goes nowhere, absolutely non-productive.
 
However, I recently noticed one guy why intensively trying to learn .NET the same way, by asking question, in somewhat effective way, asking simple but right-to-the-point, deep question. This is a rare case when a person has considerable intellect and learning abilities, which... could be used way more effectively. It already took a lot of time and nerve from experts...
 
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

// 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 Smile | :) :
struct MS {
  bool  bDataOwner;
  int   iData;
  char* pData;
  
  MS() : bDataOwner(false), iData(0), pData(NULL) {}
  ~MS() { if (bDataOwner && pData) delete pData /*use free(pData) for C allocations*/; }
};
 
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
  {
    // elements navigation
    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;
};
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100