Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

RAII, Dynamic Objects, and Factories in C++

, 3 May 2005
RAII: automatic resource management in C++.
raiifactory.zip
RAIIFactory.dsp
RAIIFactory.dsw
#ifndef MY_HIERARCHY_H
#define MY_HIERARCHY_H

/**
 * This example code is a supplement to the article 
 * 'RAII, Dynamic Objects, and Factories in C++'
 * at CodeProject: http://www.codeproject.com/
 */

#include "raiifactoryimp.h"


/**
 *  A simple base class
 */
class MyBase {
public:
   MyBase() : mi(0) {}
   MyBase (int i) : mi(i) {}
   virtual ~MyBase() {}   // virtual!!

   void set (int i) { mi = i; }
   int  get () { return mi; }
private:
   int mi;
   // non-copyable
  MyBase (const MyBase&);
  MyBase& operator= (const MyBase&);
};


/**
 *  A simple class derived from MyBase
 */
class MyDerived : public MyBase {
public:
   MyDerived() : mi(0), mf(0.0)      {}
   MyDerived (int i): mi(i), mf(0.0) {}
   virtual ~MyDerived() {}

   void set (int i) { mi = i; }
   int  get () { return mi; }
private:
   int mi;
   float mf;
   // non-copyable
  MyDerived (const MyDerived&);
  MyDerived& operator= (const MyDerived&);
};



/**
 * RAII Factory that creates MyBase objects and objects
 * derived from MyBase (e.g. MyDerived) 
 */
class MyPolymorphicFactory  {
public:
   template <typename T>
	MyBase* create() { return imp.keep (new T); }

   template <typename T>
	MyBase* create (int arg) { return imp.keep (new T (arg)); }
private:
   RaiiFactoryImp<MyBase> imp;
};


#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Roland Pibinger
Web Developer
Austria Austria
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 4 May 2005
Article Copyright 2005 by Roland Pibinger
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid