Z
Zeppe
Hi all!
my problem is this one, I think that it could be a common one, maybe a
pattern, so if you can help me somehow it would be great. Let's suppose
I have a class Base
class Base
{
// impl.
};
and a template class Foo
template <typename T>
class Foo
{
// impl.
private:
T obj_;
};
I use this class passing it as a pointer to various functions:
void bar(Foo<Base> * myFoo);
Now, I would like to derive a class
class Derived : public Base
{
// impl.
};
and pass among the functions a pointer to Foo<Derived>.
Well, how to do so? I would like to avoid the pointer in T, otherwise a
possible solution would be:
template <typename T>
class Foo
{
public:
template <typename IteratorT>
static Foo<T>* GenerateFrom(IteratorT begin, IteratorT end);
// impl.
protected:
T* obj_;
}
defining, instead of Foo<Derived>, a class
class FooDerived
: public Foo<Base>
{
public:
FooDerived()
: obj_(new Derived)
{
}
}
Ok, maybe a little bit well written to avoid the protected nude pointer.
Then none of my functions would change, being possible to pass a pointer
to FooDerived. Unfortunately, there is another issue that prevents me
from using this solution. I cannot build my FooDerived objects directly,
because I need to build them via the Foo static function GenerateFrom,
that calls the constructor of the element inside Foo objects.
I think I can delegate the construction of Foo to a factory, and pass it
as a template member of the GenerateFrom static function... but it seems
a little bit dirty solution to me.
Anyone of you has got some suggestion about a cleaner design for this
problem?
Cheers,
Zeppe
my problem is this one, I think that it could be a common one, maybe a
pattern, so if you can help me somehow it would be great. Let's suppose
I have a class Base
class Base
{
// impl.
};
and a template class Foo
template <typename T>
class Foo
{
// impl.
private:
T obj_;
};
I use this class passing it as a pointer to various functions:
void bar(Foo<Base> * myFoo);
Now, I would like to derive a class
class Derived : public Base
{
// impl.
};
and pass among the functions a pointer to Foo<Derived>.
Well, how to do so? I would like to avoid the pointer in T, otherwise a
possible solution would be:
template <typename T>
class Foo
{
public:
template <typename IteratorT>
static Foo<T>* GenerateFrom(IteratorT begin, IteratorT end);
// impl.
protected:
T* obj_;
}
defining, instead of Foo<Derived>, a class
class FooDerived
: public Foo<Base>
{
public:
FooDerived()
: obj_(new Derived)
{
}
}
Ok, maybe a little bit well written to avoid the protected nude pointer.
Then none of my functions would change, being possible to pass a pointer
to FooDerived. Unfortunately, there is another issue that prevents me
from using this solution. I cannot build my FooDerived objects directly,
because I need to build them via the Foo static function GenerateFrom,
that calls the constructor of the element inside Foo objects.
I think I can delegate the construction of Foo to a factory, and pass it
as a template member of the GenerateFrom static function... but it seems
a little bit dirty solution to me.
Anyone of you has got some suggestion about a cleaner design for this
problem?
Cheers,
Zeppe