Thread: template class // normal class

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    template class // normal class

    Hello..

    Im new to templates (some of you already know that heh) and I wonder if Im right..

    In normal class you pass arguments to constructor:

    Code:
    class somec {
      public:
    	somec(char *somearg) { .. }
    }
    
    somec *tmp = new somec(arguments);
    and in template class you do:

    Code:
    template <class t, arguments>
    class somec {
      public:
    	somec(arguments) { .. }
    	t data;
    };
    somec <char> *tmp = new somec<char, arguments>;
    Am I right about that? Thanks for help

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    14
    Your nearly right. The correct code is (green for added, red for deleted)
    Code:
    template <class t>, arguments>
    class somec {
      public:
    	somec(arguments) { .. }
    	t data;
    };
    somec <char> *tmp = new somec<char>, arguments>(arguments);

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A template class is just that: a template for a class. It doesn't create a real class until you give it template parameters to use. Let's say you have a class C:
    Code:
    class C {
      int val;
    public:
      C ( int init ): val ( init ) {}
      int get() const { return val; }
    };
    All is well and good, right? Now let's say you want class C to handle both int and char. You can make it a template class:
    Code:
    template <typename T>
    class TC {
      T val;
    public:
      C ( T init ): val ( init ) {}
      T get() const { return val; }
    };
    Everything is the same except int is replaced with T. Keep in mind that class C is a real class right now, but class TC is just a template. The class doesn't actually exist until you specialize it with a type for T. So when you do this:
    Code:
    TC<int> obj1;
    TC<char> obj2;
    Two classes are created for int and char (and one object of each is instantiated):
    Code:
    template<>
    class TC<int> {
      int val;
    public:
      TC ( int init ): val ( init ) {}
      int get() const { return val; }
    };
    
    template<>
    class TC<char> {
      char val;
    public:
      TC ( char init ): val ( init ) {}
      char get() const { return val; }
    };
    The only change is the types used. All of the behavior of the original class remains the same. The problem with your code is that you are mixing up template parameters and constructor arguments.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks guys, that made things much easier.. You're great.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Here is the code I have at the moment..

    Code:
    template <class t>
    class datalist {
    public:
    	datalist(t *init) : data(init) { }
    	char *name;
    	t *data;
    };
    
    int setdata() {
    	char *tch = new char[10];
    	strcpy(tch, "test");
    
    	int tin = 12;
    
    	datalist <char> *tmp = new datalist<char>(tch);
    	printf("data: %s\n", tmp->data);
    
    	return 0;
    }
    What I want to do is make program figure the type of argument - constructor (tch in that case) so that I could use it for integral tin too.. (without passing <char> if its possible) and then make printf figure out what type of data is it, to print it..

    Thanks a lot

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What I want to do is make program figure the type of argument - constructor
    >(tch in that case) so that I could use it for integral tin too.. (without passing
    ><char> if its possible) and then make printf figure out what type of data is it, to print it..
    No, on so many levels it would take me weeks to enumerate them all. It sounds like what you really want is a map, and you're going about getting it in the most convoluted and unsafe way imaginable. It's really as simple as this, if I understand what you want to do:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main()
    {
        map<string, int> m;
    
        m["test"] = 12; // Pair the value 12 with the key "test"
    
        cout<< m["test"] <<'\n'; // Print the value paired with the key "test"
    }
    My best code is written with the delete key.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't use printf with templates. They don't mesh. Use cout.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Prelude
    >What I want to do is make program figure the type of argument - constructor
    >(tch in that case) so that I could use it for integral tin too.. (without passing
    ><char> if its possible) and then make printf figure out what type of data is it, to print it..
    No, on so many levels it would take me weeks to enumerate them all. It sounds like what you really want is a map, and you're going about getting it in the most convoluted and unsafe way imaginable. It's really as simple as this, if I understand what you want to do:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main()
    {
        map<string, int> m;
    
        m["test"] = 12; // Pair the value 12 with the key "test"
    
        cout<< m["test"] <<'\n'; // Print the value paired with the key "test"
    }
    What I want to do is:

    I will have a program input - name with value (many possibillities):
    some_var 0,343 -> double
    some_var2 some_data -> string
    some_var3 353 -> int
    some_var4 false -> bool
    some_var5 some_data;another_data -> string list

    Now I want program to figure what type of data it is, and store it as that kind of data.. Or maybe the better option, hold an array of variable names (lets say some_var should be double) in the program with their type, so that program would automatically know what type of data should variable be.. But I dont have idea how should I do it..

    I hope you understand what I mean..

    Thanks a lot for your help masters

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But I dont have idea how should I do it..
    You seem to want a variant type in a strongly typed language. C++ is not well suited to this problem at all.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    So there is no 'good' way to solve this problem?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So there is no 'good' way to solve this problem?
    Let me put it this way: This is a problem that I would look to other tools/languages for. You can do it in C++ just like you can do just about anything in C++. That doesn't mean you should do it though. Any solution you use in C++ would be complicated and awkward. Here are a few ideas to get you started:

    1) Store everything as a string and do conversions when you need the represented type. boost::lexical_cast would be ideal for this.

    2) Derive your own wrappers around each type you intend to use from a base type class and use it polymorphically ala Java or C#.

    3) See if boost::any or boost::variant would work for you.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is another possibility. Perhaps you think you want a variant type, but in reality there is another completely different way to approach the problem. We don't know what your actual situation is, so we can't say one way or the other. You might want to think about your design again. Maybe research some design patterns and see if one of them might fit the bill and give you a way to avoid the need for the variant type.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved
    There is another possibility. Perhaps you think you want a variant type, but in reality there is another completely different way to approach the problem. We don't know what your actual situation is, so we can't say one way or the other. You might want to think about your design again. Maybe research some design patterns and see if one of them might fit the bill and give you a way to avoid the need for the variant type.
    The only thing why I want to do this is to have program's settings stored in a linked list. I was thinking about using variant types because settings will be strings or numbers and I thought it would be more professional to have variant type for different settings.. So would you recomment me to use string for every setting, and if the setting is number just convert it to int?

    Thanks for help once again

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are these settings known at compile time? In most programs I've seen, you know what the settings are when you are writing the program. If you know what the settings are, it is perfectly acceptable to store each setting in its own variable. No list or variant necessary.

    If you don't know what these settings will be when you write the program, then how do you know when you should treat them as integers and when you must treat them as strings? How do you tell the difference when you actually use these settings in your code.

    Your idea was a good thought, but unfortunately it just isn't the best choice, at least for this language.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    630
    The program will have default settings if user wont give them.. I want to store name, type and default option for each setting in an array. So you think it would be smart to use string for each?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM