Templates question

This is a discussion on Templates question within the C++ Programming forums, part of the General Programming Boards category; Say you have a templated class: Code: template <typname T> class myClass { ... }; And you want to declare ...

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Templates question

    Say you have a templated class:
    Code:
    template <typname T>
    class myClass
    {
    ...
    };
    And you want to declare two myClass objects something like this:
    Code:
    int main(void)
    {
    myClass<int> intMyObj;
    myClass<short> shortMyObj;
    
    
    }
    And you want to be able to assign and compare the two objects...
    i.e.:
    Code:
    intMyObj+=shortMyObj;
    obviously involves operator overloading, but how do you make the two types compatible?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can overload type conversions:
    Code:
    class Ojbect
    {
    public:
        
        operator Type();  //Conversion from Object to Type
        
        ...
        ...
    };

    Then you can say:

    Type a;
    Object b;

    a = b;

    Note there is no return value specified in the operator function because the target type is implicit, so the function must return an object of type Type.
    Last edited by 7stud; 08-12-2003 at 10:41 PM.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Hm that doesn't seem to work, are you sure thats right for templated classes?

    I'm getting "no match for..." errors when i try to use my overloaded assignment operator
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by 7stud
    Note there is no return value specified in the operator function because the target type is implicit, so the function must return an object of type Type.
    I don't think so. Maybe you made a mistake somewhere?

    One way to solve the problem is to use more templates:
    Code:
    template<typename T, typename A>
    myClass<T>& operator+=(const myClass<A>& a) {
      this->value += a.value;
    
    }

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ok thanks I'll try that but another question. What if I want to static_cast<myClass>. What needs to be done before this is possible (or is it even possible)? For example:
    Code:
    char c='c';
    myClass<int> x=static_cast<myClass>c;
    Edit: Hm your example isnt working:
    Code:
    template <typename T,typename U>
    myClass<T> myClass<T>::operator= ( myClass<U> & two)
    {
    if (this==&two)
      return (itsVal);
      
    return (itsVal=two.getitsVal()) ; 
    }
    Errors (dont make sense...):

    64 C:\Dev-Cpp\dd.cpp
    prototype for `Integer<T>
    candidate is C:\Dev-Cpp\dd.cpp:13
    Integer<T>
    64 C:\Dev-Cpp\dd.cpp
    template definition of non-template `Integer<T>
    Last edited by JaWiB; 08-12-2003 at 11:12 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I don't think so. Maybe you made a mistake somewhere?"

    Here's an example:
    Code:
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
    class Test
    {
    public:
    	double a;
    
    	operator char*()
    	{
    		return "Hello world.";
    	}
    };
    
    
    int main()
    {	
    
    	Test my_test;
    	my_test.a = 10.0;
    
    	cout<<setprecision(2)<<fixed;
    	cout<<my_test.a<<endl;
    
    	
    	char* pch;
    	pch = my_test;
    	cout<<pch<<endl;
    	
    
    	return 0;
    
    }
    You can also use constructors to implement conversions:

    Type2(const Type1& a); //coverts from Type1 to Type2
    Last edited by 7stud; 08-12-2003 at 11:59 PM.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Both 7stud's and ygf's codes will work, but personally, I'd avoid both of them. Essentially what you are doing is adding type-specialized code to something which is generalized (namely, a templated class such as this).

    7stud's would cause a proliferation of code (which is what templates are partially designed to avoid), and ygf's could be the cause of some very subtle errors (truncation errors from the cast operation ... which probably should have a 'static_cast' to make it clear).

    Anyways, what I'd advise is a more generic solution like the following :

    Code:
    #include <functional>
    
    template<typename T>
    class test {
    public:
      T get_value() const { return value; }
    
      template<typename A, typename Func>
      void assign(test<A>, Func);
    private:
      T value;
    };
    
    template<typename T> template<typename A, typename Func>
    void test<T>::assign(test<A> src, Func convert) {
      value = convert(value, src.get_value());
    }
    
    struct convert_char : public std::binary_function<int, char, int> {
      int operator()(int x, char y) { return x + static_cast<int>(y); }
    };
    
    int main() {
      test<int> obj_a;
      test<char> obj_b;
    
      obj_a.assign(obj_b, convert_char());
    }
    The convert type in this case is a binary functor which takes the value of the object being assigned to first, and then the next parameter is the value of the second object. It is still possible to get around some of the conversion rules (i.e. poor casting practices with primitive types), but in this case, that is hard to do accidently. Also, this preserves the generic nature of the class (you aren't adding type-specialized code to it).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Templates question
    By tezcatlipooca in forum C++ Programming
    Replies: 9
    Last Post: 12-30-2006, 01:08 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Stack Question With Templates
    By Anonymous Freak in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2003, 11:18 AM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

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