D
Dewdman42
I have a question for C++ gurus
Assuming I have template class:
template <class T>
class Car<T>
{
Car();
virtual T* factory();
}
template <class T>
T*
Car::factory()
{
return new T;
}
Here is a class I will use to drive T above:
class Airplane
{
Airplane();
}
To use like this:
Car<Airplane> myCar;
Airplane* a = myCar.factory();
Fine so far. Now let's say I want to subclass my template to provide
an ovreride to the factory method because I don't want to allow a
default constructor of Airplane, but rather a one arg constructor:
class Spaceship
{
Spaceship(int arg);
private:
Spaceship(); // to prevent being able to instantiate without
arg
}
And finally a subclass of the template that overrides the virtual
factory method:
class Boat : public Car<Spaceship>
{
Boat();
virtual Spaceship* factory();
}
Spaceship*
Boat::factory()
{
return new Spaceship(100);
}
In theory to use like this:
Boat<Spaceship> myBoat;
Spaceship* a = myBoat.factory();
The above does not quite compile because when the compiler uses
Spaceship to generate the Boat<Airplane> class from the template,
which is really a subclass of Car, it expects to find a default
constructor for the Spaceship class in order to satisfy the template
base class virtual factory method, even though there are no clients
that will use Car<Spaceship>, only Boat<Spaceship>
I really need Car() to have this default factory method for cases
where people will not use a subclass or override that virtual factory
method, like my first example of Car<Airplane>. However, I also in
this case don't want to provide a default constructor to my Spaceship
class because it will NEVER be used that way and I want to make sure
it can't be used that way.
Can anyone think of a way to satisfy the template without having to
add the default constructor to Spaceship? Or perhaps a different
class structure that will accomplish what I'm trying to do. ??
Assuming I have template class:
template <class T>
class Car<T>
{
Car();
virtual T* factory();
}
template <class T>
T*
Car::factory()
{
return new T;
}
Here is a class I will use to drive T above:
class Airplane
{
Airplane();
}
To use like this:
Car<Airplane> myCar;
Airplane* a = myCar.factory();
Fine so far. Now let's say I want to subclass my template to provide
an ovreride to the factory method because I don't want to allow a
default constructor of Airplane, but rather a one arg constructor:
class Spaceship
{
Spaceship(int arg);
private:
Spaceship(); // to prevent being able to instantiate without
arg
}
And finally a subclass of the template that overrides the virtual
factory method:
class Boat : public Car<Spaceship>
{
Boat();
virtual Spaceship* factory();
}
Spaceship*
Boat::factory()
{
return new Spaceship(100);
}
In theory to use like this:
Boat<Spaceship> myBoat;
Spaceship* a = myBoat.factory();
The above does not quite compile because when the compiler uses
Spaceship to generate the Boat<Airplane> class from the template,
which is really a subclass of Car, it expects to find a default
constructor for the Spaceship class in order to satisfy the template
base class virtual factory method, even though there are no clients
that will use Car<Spaceship>, only Boat<Spaceship>
I really need Car() to have this default factory method for cases
where people will not use a subclass or override that virtual factory
method, like my first example of Car<Airplane>. However, I also in
this case don't want to provide a default constructor to my Spaceship
class because it will NEVER be used that way and I want to make sure
it can't be used that way.
Can anyone think of a way to satisfy the template without having to
add the default constructor to Spaceship? Or perhaps a different
class structure that will accomplish what I'm trying to do. ??