|
Boost : |
Subject: [boost] typeof or similar
From: Steffen Roeber (Steffen.Roeber_at_[hidden])
Date: 2010-02-23 04:50:59
Hi,
I have 3 classes:
1.
class Base
{
public:
virtual Base *createDerived()
{
return 0;
}
};
2.
template <class T>
class BaseT : public Base
{
public:
virtual Base *createDerived()
{
return new DerivedT<T>(); //nicht smart, because DerivedT must be known
}
};
3.
template <class T>
class DerivedT : public BaseT<T>
{
};
So far so good. Or not. But now the problems are starting.
Base *base = new BaseT<int>;
This object it created at a place that is not known where I want to create:
Base *derived = base->createDerived(); //base is a Base *
The result is an object that has the same template type as 'base'.
Finally I got my desired result, but the problem is the relation downward
relation between BaseT and DerivedT.
My preferred way would be something like a factory:
class Factory
{
public:
template <class T>
static Base *createDerived<T>()
{
return new DerivedT<T>();
}
};
But how can I tell the factory which type to be used.
Base *derived = Factory::createDerived<sameAsBase> (base); //base is a Base
*
I think there is no "normal" C++ language support. But Boost has some
methods that by-pass some language constraints. I took a loot at something
like 'typeof'. But I don't get the things together.
Has anybody any idea? Or is it impossible to do this?
Kind regards
Steffen
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk