Results 1 to 5 of 5

Thread: Dynamic template

  1. #1
    Join Date
    Jun 2008
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Dynamic template

    Hi

    I have a problem that depends on template class. I have declaration of class, for example:

    template<typename T> class A;

    And I want to create a variable, but I want to decide later what will be template argument (user will be able to select between int, float etc). I can't find good solution, i tried pointer:

    A *instance;

    and later: instance = new A<float>(); but there is compiler error in declaration(missing template argument). I also tried void pointer, it works with creating objects, but later I'm unable to call any method from class A.

    If you have some idea please help me

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic template

    Show us your implementatin of A.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Jun 2008
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic template

    The problem don't depends on implementation of class (unless there is some design pattern that may be usefull), you can have the simplest example of class with template argument. The qestion is: how to create an object, and decide later what will be the tempalte argument

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

    Default Re: Dynamic template

    Use inheritance.
    Qt Code:
    1. class Base {
    2. //...
    3. };
    4.  
    5. template<typename T> class A : public Base {
    6. //...
    7. };
    8.  
    9. Base *var;
    10. var = new A<int>;
    To copy to clipboard, switch view to plain text mode 

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

    seth___ (11th September 2008)

  6. #5
    Join Date
    Jun 2008
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic template

    Thanks, thats it.

Similar Threads

  1. Replies: 2
    Last Post: 7th July 2008, 08:44
  2. specialization of a template method
    By darksaga in forum General Programming
    Replies: 3
    Last Post: 12th April 2008, 00:53
  3. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 07:43
  4. how to write a template function in a form..
    By nass in forum General Programming
    Replies: 7
    Last Post: 18th December 2006, 19:36
  5. problem using template
    By mickey in forum General Programming
    Replies: 6
    Last Post: 18th November 2006, 15:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.