Results 1 to 16 of 16

Thread: Template classes and abstraction of type

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Template classes and abstraction of type

    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.

    Qt Code:
    1. /*Use of the classes in the main program*/
    2.  
    3. //Check for type of files and allocate based on type
    4. VolumeGeneral::type imageType;
    5.  
    6. //Determine the type based on input files
    7. ...
    8.  
    9. //Allocate with righ type
    10. VolumeGeneral* volume = 0;
    11.  
    12. if (imageType == typeU16)
    13. volume = new VolumeSpecified<unsigned short>();
    14.  
    15. else if (imageType == typeU8)
    16. volume = new VolumeSpecified<unsigned char>();
    17.  
    18. else
    19. break;
    20.  
    21. //Perform general operations
    22. volume->init();
    23. volume->doSomething();
    24.  
    25. //Perform type specific operations
    26. if (volume->getType() == VolumeGeneral::typeU16)
    27. {
    28. VolumeSpecified<unsigned short>* volumeS = dynamic_cast<VolumeSpecified<unsigned short>*>(volume);
    29. unsigned short testVariable = volumeS->getData();
    30. }
    31.  
    32. 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 ...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Template classes and abstraction of type

    Qt Code:
    1. class Image {
    2. public:
    3. virtual void init() = 0;
    4. virtual void doSomething() = 0;
    5. virtual ~Image(){}
    6. };
    7.  
    8. class Image16 : public Image {
    9. public:
    10. Image16(unsigned short *data){
    11. //...
    12. }
    13. void init(){ ... }
    14. void doSomething(){ ... }
    15. };
    16.  
    17. class Image8 : public Image {
    18. //...
    19. };
    20.  
    21. Image *img;
    22. if(type==16)
    23. img = new Image16(data);
    24. else img = new Image8(data);
    25. img->init();
    26. img->doSomething();
    To copy to clipboard, switch view to plain text mode 

    Isn't that what you want? You don't reference the actual class type anywhere apart from calling the constructor, so there is no problem of not knowing the type.

  3. #3
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Template classes and abstraction of type

    Thank you for that code wysota. However, it seems like I am basically doing the same thing. The only difference I see is that your solution has less code because you do not keep track of the type in your base class. However, I sometimes do need to do some type-specific operations (like in the example) to interface with the main program, so I have to keep track of it to get out the right type of data.

    In conclusion, this is already a lot cleaner than what I started from. The only thing I need to do is to add all non-type specific methods as pure virtual methods to the base class.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Template classes and abstraction of type

    Quote Originally Posted by Raistlin View Post
    However, I sometimes do need to do some type-specific operations (like in the example) to interface with the main program, so I have to keep track of it to get out the right type of data.
    Qt Code:
    1. if((Image16 *image16 = dynamic_cast<Image16*>(img))!=0){
    2. image16->specificMethod16();
    3. } else if((Image8 *image8 = dynamic_cast<Image8*>(img))!=0){
    4. image8->specificMethod8();
    5. } else {
    6. std::cerr << "Unknown Image subclass encountered" << std::endl;
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to wysota for this useful post:

    Raistlin (1st April 2008)

  6. #5
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Template classes and abstraction of type

    Oh, thanks for that addition. I did not know casting to the wrong type returned 0 instead of a junk value.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Template classes and abstraction of type

    That's the idea of dynamic_cast. If your Image class inherited QObject, you could also use qobject_cast.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.