Click here to Skip to main content
11,934,809 members (50,416 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ Visual-Studio STL
i have an base class like this

class Base
    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;

I added the structure in list stl as
std::list< MYSTRUCT* > SS1;
     List  = new MYSTRUCT;
    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
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.
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...


1 solution

Rate this: bad
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
  virtual ~lstOwnerMS() { Empty(); }
  void AddData(const MS* 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;
    } else {
      iterPMS = NULL;
    return pResult;
  void Empty() {
    while (!lstPMS::empty()) {
      delete lstPMS::front();
class IBase
  virtual void ProcessPMSList(lstOwnerMS& lst) const = 0;
class Test : public IBase
  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, ".");
  Test test;
  return 0;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2015
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