Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Typecasting operator on simple types vs. classes
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Gianni Mariani, post: 1462487"] I'm not sure why the code above does not work. There seems to be only one conversion operator and one operator[] definition. I looked through the copy of the standard I have and it is unfortunatly very wordy about the subject and hard to filter the relevant pieces. This is another version of your example that does work. (at least on gcc 3.3.1). template <class mytype> class TestProxy; template <class mytype> class Test { friend class TestProxy<mytype>; mytype v; public: operator mytype & () { return v; } TestProxy<mytype> operator [] (int i); }; template <typename T> class PtrType { public: typedef int Type; // HACK ALERT }; template <typename T> class PtrType<T*> { public: typedef T & Type; }; template <class mytype> class TestProxy { public: Test<mytype> & m_test; int m_i; TestProxy( Test<mytype> & i_test, int i ) : m_test( i_test ), m_i( i ) { } operator typename PtrType<mytype>::Type () { return m_test.v[m_i]; } }; template <class mytype> inline TestProxy<mytype> Test<mytype>::operator [] (int i) { return TestProxy<mytype>( *this, i ); } class Test2 { public: int operator [] (int i) { return i; } }; int main(void) { Test<double*> t1; Test<Test2> t2; t1[1]; // This line works fine int x = t2[2]; // This line doesn't compile ((Test2)t2)[3]; // This also works return 0; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Typecasting operator on simple types vs. classes
Top