FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
October 01, 2001

The Standard Librarian: I/O and Function Objects: Containers of Pointers

(Page 2 of 2)
October 2001 C++ Experts Forum/The Standard Librarian/Listing 1

Listing 1: A simple arena class

#include <vector>

class arena {
private:
  struct holder {
    virtual ~holder() { }
  };

  template <class T>
  struct obj_holder : public holder {
    T obj;

    template <class Arg1>
    obj_holder(Arg1 arg1)
      : obj(arg1) { }
  };

  std::vector<holder*> owned;

private:
  arena(const arena&);
  void operator=(const arena&);

public:
  arena() { }
  ~arena() {
    destroy_all();
  }
  
  template <class T, class Arg1>
  T* create(Arg1 arg1) {
    obj_holder<T>* p = new obj_holder<T>(arg1);
    owned.push_back(p);
    return &p->obj;
  }

  void destroy_all() {
    std::vector<holder*>::size_type i = 0;
    while (i < owned.size()) {
      delete owned[i];
      ++i;
    }
    owned.clear();
  }
};

Previous Page | 1 | 2
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK