CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Feb 2009
    Posts
    326

    conversion operator in template

    Hi,

    Brief description:
    ---------------------
    I have a conversion operator that seems to work ok when used without templates.
    But the conversion operator doesn't seem to be working when used with templates.

    Example (complete code is given below):
    ------------------------------------------------
    I have 2 template classes, ClassA<T> and ClassB<T>

    ClassB has a conversion operator to convert ClassB to "const ClassA<T>* const"

    Function f1 - without template
    ---------------------------------------
    Code:
    void f1(const ClassA<int>* const pPtrY).
    When this function f1 is invoked by passing an object of ClassB<int>, the conversion operator gets invoked, and it is converted to "const ClassA<int>* const".

    This works as expected

    Function f2 - with template
    ----------------------------------
    Code:
    template<typename T>
    void f2(const ClassA<T>* const pPtrY)
    When this function f2 is invoked by passing an object of ClassB<T>, the conversion operator doesn't get invoked and hence throws an error.


    Doubt
    --------
    Why does the conversion operator not work with templates ?
    What is the underlying concept ?
    Or am I missing something ?

    complete code given below:
    Code:
    #include<iostream>
    
    template<typename T>
    class ClassA
    {};
    
    template<typename T>
    class ClassB
    {
        public:
            ClassB();
            ClassB(ClassA<T>* const pPtrX);       //Copy Constructor
            operator const ClassA<T>* const () const;   //Conversion Operator
    
        private:
            ClassA<T> *ptrX;
    
    };
    
    void f1(const ClassA<int>* const pPtrY)
    {}
    
    template<typename T>
    void f2(const ClassA<T>* const pPtrY)
    {}
    
    int main()
    {
        
        ClassA<int> *p1 = new ClassA<int>;
        ClassB<int> b1(p1);
        
        f1(b1);    //works ok, when without a template
                   //conversion operator is invoked to convert b1 to ClassA<T>*
        
        //f2(b1);  //throws an error, when with a template
                   //conversion operator is not invoked, can't understand why
                   //what is the reason and what is the undelying concept ?
        return(0);
    }
    
    template<typename T>
    ClassB<T> :: ClassB()
        : ptrX(NULL)
    {}
    
    template<typename T>
    ClassB<T> :: ClassB(ClassA<T>* const pPtrX)
        : ptrX(pPtrX)
    {}
    
    template<typename T>
    ClassB<T> :: operator const ClassA<T>* const () const
    {
        return(ptrX);
    }
    Last edited by Muthuveerappan; July 13th, 2009 at 01:06 AM.

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