Thread: invoking conversion via .operator

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    invoking conversion via .operator

    Code:
    class MyClass {
    public:
      template <typename T> operator T& () { /* code here */ }
    }
    
    int my_func()
    {
      MyClass mc;
    
      mc.operator int& (); // !! fails to compile: error: no matching function for call to ‘MyClass::operator T&()’
    }
    i need to invoke the conversion that way: object.operator int&
    (the reason is that im using bison, and trying to hack that damn stack/union stuff...)
    (int&)object works though - but i can't use that.

    it seems that the template argument needs to be specified in order to explicitely instanciate a function template. how can that be done?
    signature under construction

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    template<class T>
    class MyClass {
    public:
      operator & () { /* code here */ } //+,-,[],* and many more are valid operators
    }
    
    int my_func()
    {
      MyClass mc;
    
      mc.operator int& (); // !! fails to compile: error: no matching function for call to ‘MyClass::operator T&()’
    }
    Im not sure if you can specifiy operators with templates

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    me neither ^^

    but i still have slight hope that it is possible


    edit: um... i mean the code works if i use the usual c-style cast syntax:
    (double&)object, (float&)object, etc... so operators and templates actually work.
    im not sure wheter that "alternative" syntax (something like object.operator type ()) works
    Last edited by Raven Arkadon; 12-05-2005 at 10:19 PM.
    signature under construction

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Is this what you are talking about?

    Code:
    template<class T>
    class myClass
    {
        public:
            T operator &()
            {
                return data;
            }
        private:
            T data;
    };
    
    
    int main()
    {
        myClass<int> mc;
        mc.operator &();
    }
    Or...
    Code:
    class myClass
    {
        public:
            template<class T>
            T operator &()
            {
                return data;
            }
        private:
            int data;
    };
    
    
    int main()
    {
        myClass mc;
        mc.operator &<int>();
    }
    Last edited by prog-bman; 12-06-2005 at 10:43 AM.
    Woop?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    lol, simply overloading the reference operator, ya, thats what i was looking for.
    thxalot!
    signature under construction

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's the addressof operator, not reference operator. (The reference "operator" is not overloadable.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM