Re: Template subclass virtual override
On Aug 7, 6:46 am, Dewdman42 <dewdma...@gmail.com> wrote:
I have a question for C++ gurus
Assuming I have template class:
The code posted is far far awaty from being a compilable code.
template <class T>
virtual T* factory();
Everything in this class is private. Not in sync with the way 'Car' is
used in your remaining code.
template <class T>
Doesnot compile. Car is a template class, you should use Car<T>
return new T;
Here is a class I will use to drive T above:
private constructor - not in sync with usage of Airplane below. Also
To use like this:
Can't instantiate, Car constructor is private
Airplane* a = myCar.factory();
Can't call, factory() is private
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:
Spaceship(); // to prevent being able to instantiate without
Everything here is private, no need of that label. Further, you don't
need to declare a non-arg constructor as private if you have declared
And finally a subclass of the template that overrides the virtual
class Boat : public Car<Spaceship>
virtual Spaceship* factory();
Everything private, missing ;
return new Spaceship(100);
In theory to use like this:
Boat is _not_ a template class. You cant use it like this. Further,
Boat constructor is private
Spaceship* a = myBoat.factory();
Boat::factory is private
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>
Assuming that all the above errors are taken care of, observe that the
constructor Boat() will need the class Car<Spaceship> to get
instantiated. The class Car<T> has a virtual function factory(). Acc.
to 14.7.1(9): "It is unspecified whether or not an implementation
implicitly instantiates a virtual member function of a class template
if the virtual member function would not otherwise be instantiated".
If your compiler decides to instantiates the virtual member function,
that attempt will fail since there is no Spaceship::Spaceship()
constructor. (public of course).
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. ??
Please post code that compiles. That will help.