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="Nicolay Korslund, post: 1462439"] Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator returns a 'mytype'. In main() I use the [] operator on 'Test'-class objects. If 'mytype' is a pointer the [] operator works fine. But if I instead try to call an overloaded [] operator, the typecast operator is not invoked, and I get a compiler error (unless I make an explicit cast.) So does the 'operator mytype ()' only work properly with simple types? Any help would be appreciated! Nico template <class mytype> class Test { mytype i; public: operator mytype () {return i;} }; class Test2 { public: int operator [] (int i) {return i;} }; int main(void) { Test<double*> t1; Test<Test2> t2; t1[0]; // This line works fine t2[0]; // This line doesn't compile ((Test2)t2)[0]; // This also works return 0; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Typecasting operator on simple types vs. classes
Top