Click here to Skip to main content
15,891,921 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
i have an base class like this

C++
class Base
    {
    public:
    virtual void fun() const =0;
    };

Derived class like this :

C++
class Derived:public Base
  {
  virtual void fun()
  {
  //implemtation of fun
  }
  };



I had an global structure


C++
struct Mystruct
  {
  int a;
  char *b;
  }MYSTRUCT;


I added the structure in list stl as
C++
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
Comments
Richard MacCutchan 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 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 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 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

// 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 :) :
C++
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;
};
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900