Template problem (maybe)

This is a discussion on Template problem (maybe) within the C++ Programming forums, part of the General Programming Boards category; Hi maybe the solution would be using templates,and I just forgot how to use them, but right now I have ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    Question Template problem (maybe)

    Hi

    maybe the solution would be using templates,and I just forgot how to use them, but right now I have the following problem:

    I want to write a set of functions, so I can operate on vector<int>s as if they were integers, meaning addition, subtraction, comparing, dividing etc. But to be flexible (i.e. if one of the two or both provided integer arguments fit in 2^32-1, like loop variables), I want to allow for any mix of vectors and integers in all of the function arguments.

    example:
    Code:
    vector<int> add(vector<int> a, vector<int> b);   //0
    vector<int> add(vector<int> a, int b);           //1
    vector<int> add(int a, vector<int> b);           //2
    vector<int> add(int a, int b);                   //3
    Basically the true INTs in functions 1-3 go to a conversion function which returns the respective vector<int>, then these are provided to function 0. Same for all other functions.

    Now my question would be: Can I somehow simplify this process of converting from int to vector<int> and calling the correct function, other than by overloading?
    Last edited by SnowCrash; 06-09-2004 at 10:34 PM.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    If you were to use templates, I would imagine the code within the function resembling a state machine that checks which template was used. I have found that having complex templates pays off most when you have several levels of inheritence. That's just my humble input.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can I somehow simplify this process of converting from int to vector<int> and calling the correct function, other than by overloading?
    Yes, create an integer type that does it for you:
    Code:
    class Integer {
    public:
      Integer(int init);
      Integer(vector<int> init);
      ...
    };
    
    Integer add(const Integer& a, const Integer& b);
    Conveniently enough, this approach gives you more power than they way you were going about it. You can make the Integer class as complex or as simple as you like while protecting client code from the complexity.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 05:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  5. Problem with template usage
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 06:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21