RE:Re: memfunc template

From: Masaharu Goto (MXJ02154@nifty.ne.jp)
Date: Thu Feb 10 2000 - 14:29:20 MET


Hello George,

I might have missed a point in previous message. I understood you want
FactoryFunction() have compiled. So it is not cint issue. Your point
is on localobj[2]=p;

Good news is that seems like it is possible with global function
template. Adding following definition in cint standard header file,
your example should work.

----------------------------------------------------------
template<class T,class X>
auto_ptr<T>& operator=(auto_ptr<T>& a,auto_ptr<X>& b) 
{
  a = auto_ptr<T>(b.get());
  b.release();
}
-----------------------------------------------------------

Looking at this from a little differnt aspect. Apart from cint.
I usually deal with such case with AutoContainer adapter. This
is less flexible but more efficient and can use STL containers.

--------------------------------------------------------
#include <algorithm>
using namespace std;

template <class T>
struct DeleteObj : unary_function<T,T> {
  T operator()(const T& x) const {delete x; return 0;}
};

template<class pObjBase,class Container>
struct AutoContainer : public Container {
  ~AutoContainer()
    {transform(rbegin(),rend(),rbegin(),DeleteObj<pObjBase>());}
};

class TObject { };
class Base1: public TObject {};
class X: public Base1 {};

Base* FactoryFunction() { return new X; }

main() {
  AutoContainer<Base1*,vector<Base1*> > localobj(10);
  localobj.push_back(FactoryFunction());
}
---------------------------------------------------------

The adapter concept appears in STL stack and queue container.
If you would like, you can add pop functions in AutoContainer.
I wonder if this makes more sense to your application. 

Thank you
Masaharu Goto

>class X: public Base1 {};
>
>class Base1: public TObject {};
>
>auto_ptr<Base1 *> FactoryFunction() { return new X; }
>
>and in CINT:
>
>auto_ptr<Base1 *> p = FactoryFunction(); 
>auto_ptr<TObject *> localobjs[10];  
>// localobjs is a polymorphic list of resources I own to be cleaned up
>// appropriately. I'm not using an STL container here because they don't
>// work well with auto_ptr :(
>localobjs[2] = p;                    



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:18 MET