CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    class template Convert which can convert itself to any data type

    Code:
    template<class T>
    class Convert
    {   
       T data;
    public: 
       Convert(const T& tData = T()) : data(tData)
       { }
    
       template<class C>   
       bool IsEqualTo( const C& other ) const      
       {        
           return data == other;   
       }
    };
    Code:
    template<class T>
    operator T() const
    {
        return data;
    } 
    
    Convert<int> IntData(40);
    float FloatData;
    double DoubleData;
    
    FloatData = IntData;
    DoubleData = IntData;
    Code:
    Convert<int>::operator<float> float();
    Convert<int>::operator<double> double();
    ) ----------------------------------------------------
    Why do we use operator? Is float and double function names below?
    Code:
    Convert<int>::operator<float> float();
    Convert<int>::operator<double> double();
    Thanks in advance
    Last edited by loves_oi; September 23rd, 2012 at 07:54 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: class template Convert which can convert itself to any data type

    Quote Originally Posted by loves_oi View Post
    Code:
    template<class T>
    operator T() const
    {
        return data;
    }
    This doesn't make sense. The cast operator can only be defined as a class member function.
    What are you trying to achieve at a higher level?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured