Thread: template operators

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    13

    template operators

    Hi !

    I'd like to know whether it's possible, for an operator, to be a template (using vc++6). More specifically, suppose I've got a template class

    Code:
    template <class T> class Pointer <T>
    { T *p;
    }
    I'd like to have an operator that converts type T* to type Pointer <T>. But vc++ does not allow the following syntax :
    Code:
    template <class T> operator Pointer <T> (T *p);
    So, what should I write instead ?
    Thanks for your help !

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    I don't know if you can template an operator overload function. But I don't see why not since it's a function just like any other... apart from the fact it uses the 'operator' keyword.

    Nevertheless your declaration has a syntax error. You cannot create new operators. Just overload the existing ones and those of them that are allowed. 'Pointer' is what most probably is giving you the error. Replace it by a known and accepted operator. Take in mind that you can't overload the * pointer operator. Since i'm not that familiar yet with operator overloading I can't help you any further.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Thanks for your reply, Mario. But what should I write instead ? I don't see any other possible syntax !

  4. #4
    Unregistered
    Guest
    Here's some code. If you just want to cast
    I think I would just use a cast instead of this code.
    Otherwise if you actually want to use a T* to create a Pointer<T>
    make a constructor like Pointer(T* tptr).

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    template <class T>
    class Pointer {
    public:
            int get_i() const { return i; }
    private:
            int i;        
    };
    
    template <class T>
    Pointer<T>* pointer_cast(const T* tptr)
    {
            return (Pointer<T>*)tptr;
    }
    
    int main(void)
    {
            Pointer<int>* p;
            int i = 4;
    
            p = pointer_cast<int>(&i);
    
            // since most implementations don't waste space this should print 4
            cout << p->get_i() << endl;
    
            return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Aaah, thanks ! I didn't know that the pointer_cast keyword did exist.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Ooops, I've just understood that pointer_cast was not a keyword, but a function's name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Template specialization
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 02:02 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM