I'm trying to create a workaround using implied pointers to obscure most of the internals of a library whilst still retaining basic functionality. Currently I'm relying on templates and the code below produces the following compiler error that I don't know how to solve:
prog.cpp: In function ‘int main()’:
prog.cpp:55:10: error: no match for ‘operator=’ (operand types are ‘std::auto_ptr<TypeAdapter_impl<mytype> >’ and ‘TypeAdapter_impl<mytype>*’) t._impl = tam;
Here is the code:
#include <iostream>
#include <memory>
using namespace std;
typedef long document_ptr;
class mytype {
public:
mytype(document_ptr d) {
a = d;
std::cout << "Instantiated mytype with document_ptr " << a << "\n";
}
document_ptr a;
};
class TypeContainer {
public:
void * _t;
TypeContainer(void * t) { _t = t; }
~TypeContainer() { delete _t; }
// TODO operator *
};
class DocumentContainer {
public:
document_ptr * doc;
};
template<class Type>
class TypeAdapter_impl
{
public:
TypeAdapter_impl() { }
~TypeAdapter_impl() { }
TypeContainer TypeFactory(DocumentContainer& d){
Type * t = new Type(d.doc);
return TypeContainer(t);
}
};
template<class Type>
class TypeAdapter
{
public:
std::auto_ptr< TypeAdapter_impl<Type> > _impl;
};
int main() {
// your code goes here
TypeAdapter<mytype> t;
TypeAdapter_impl<mytype> * tam = new TypeAdapter_impl<mytype>;
t._impl = tam;
DocumentContainer d;
d.doc = new document_ptr(10);
mytype m = t._impl->TypeFactory(d);
return 0;
}
Any help would be much appreciated!