I modified the example a bit to make it a bit more clear. I use a single value to type T here for simplicity, but of course this not what happens in reality. But it is just to illustrate the idea.
/*Use of the classes in the main program*/
//Check for type of files and allocate based on type
VolumeGeneral::type imageType;
//Determine the type based on input files
...
//Allocate with righ type
VolumeGeneral* volume = 0;
if (imageType == typeU16)
volume = new VolumeSpecified<unsigned short>();
else if (imageType == typeU8)
volume = new VolumeSpecified<unsigned char>();
else
break;
//Perform general operations
volume->init();
volume->doSomething();
//Perform type specific operations
if (volume->getType() == VolumeGeneral::typeU16)
{
VolumeSpecified<unsigned short>* volumeS = dynamic_cast<VolumeSpecified<unsigned short>*>(volume);
unsigned short testVariable = volumeS->getData();
}
delete volume;
/*Use of the classes in the main program*/
//Check for type of files and allocate based on type
VolumeGeneral::type imageType;
//Determine the type based on input files
...
//Allocate with righ type
VolumeGeneral* volume = 0;
if (imageType == typeU16)
volume = new VolumeSpecified<unsigned short>();
else if (imageType == typeU8)
volume = new VolumeSpecified<unsigned char>();
else
break;
//Perform general operations
volume->init();
volume->doSomething();
//Perform type specific operations
if (volume->getType() == VolumeGeneral::typeU16)
{
VolumeSpecified<unsigned short>* volumeS = dynamic_cast<VolumeSpecified<unsigned short>*>(volume);
unsigned short testVariable = volumeS->getData();
}
delete volume;
To copy to clipboard, switch view to plain text mode
I don't see how I can do this without the extra class. If I only have my template class, I always need to specify the type, right? I can not use an object of type template class T without specifying T. So I need a base class which does not have any type to provide an interface.
What do you mean by using a 'base class pointer' when you say I don't need a wrapper? It seems like I need to list all methods in my abstract base class, since I need it as interface for my methods. If you talk about casting some base class pointer to the right template type, it seems this is something I always need to do, regardless whether I am exporting or importing data, or I am doing something where the type does not need to be visible.
Maybe I just need some example code on how to apply what you mean in my case, since it seems I am missing out on something ...
Bookmarks