D
Dave
Hello all,
Please see my question embedded in comment form below.
Thanks,
Dave
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
using namespace boost;
// A base class
class Deleter
{
public:
Deleter() {}
virtual ~Deleter() {}
};
// A derived class
class MyClass: public Deleter
{
public:
MyClass(int data): m_data(data) {}
virtual ~MyClass() {}
void DoIt() const
{
cout << m_data << endl;
}
private:
int m_data;
};
// A factory for objects that are constructed with 0 or 1 parameters.
template <class T>
class Factory
{
public:
static shared_ptr<T> Create()
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T());
}
template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};
// Here's where I don't have things right. I'd like to specialize Factory
for types derived from Deleter.
// How may I do so?
template <class T> // ?????????????
class Factory<Deleter> // ?????????????
{
static shared_ptr<T> Create()
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T());
}
template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};
int main()
{
shared_ptr<MyClass> ptr(Factory<MyClass>::Create(42));
ptr->DoIt();
}
Please see my question embedded in comment form below.
Thanks,
Dave
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
using namespace boost;
// A base class
class Deleter
{
public:
Deleter() {}
virtual ~Deleter() {}
};
// A derived class
class MyClass: public Deleter
{
public:
MyClass(int data): m_data(data) {}
virtual ~MyClass() {}
void DoIt() const
{
cout << m_data << endl;
}
private:
int m_data;
};
// A factory for objects that are constructed with 0 or 1 parameters.
template <class T>
class Factory
{
public:
static shared_ptr<T> Create()
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T());
}
template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};
// Here's where I don't have things right. I'd like to specialize Factory
for types derived from Deleter.
// How may I do so?
template <class T> // ?????????????
class Factory<Deleter> // ?????????????
{
static shared_ptr<T> Create()
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T());
}
template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};
int main()
{
shared_ptr<MyClass> ptr(Factory<MyClass>::Create(42));
ptr->DoIt();
}