Skip to main content
GameDev.net gamedev.net
🔒 Locked

Type conversion operator

Started by LilBudyWizer Dec 21, 2002 at 2:23 AM 2 replies 200+ views
Original Post
LilBudyWizer
LilBudyWizer
For class T1 I can define a type conversion operator to T2 as T1::opeator T2(). I try it and it works as expected. How do you do a pointer though? The compiler accepts T1::operator T2*(), but it doesn''t actually call it when I try T2 *pt2 = (T2 *)pt1 or T2 *pt2 = pt1. When I try it as a non-member function I get an error saying it must be a member function. If you are wondering why... Another post on here someone''s compiler isn''t correctly casting pointers to a derived class with multiple bases to pointers to the secondary bases. It is possible to do it explicitly which then makes it nice if you can add the operator to the derived class. It''s a hack, but at least on a compiler that does the cast correctly you could just remove the operator from the class definition. With anything you would have to actually change the code using the class rather than just the class.
Keys to success: Ability, ambition and opportunity.
Neophyte
Neophyte
It won''t work if the variable you''re casting (pt1 in your example) is a pointer unless you dereference it.
For instance:

  
class T2 {
...
};

class T1 {
public:
operator T2*() { ... }
};

T1* t1 = new T1();
T2* t2 = (T2*)*t1;


In the case where T1 is derived from T2 you could for instance implement the casting operator something like this:

  
T1::operator T2*() { return (T2*)*this; }



The reason for this is that the default behaviour (as far as I can determine) when casting between two pointer types is to do a direct pointer-typecast without referring to explicitly defined member typecast operators.


-Neophyte
LilBudyWizer
LilBudyWizer
Sure enough. Counter-intuitive, but makes sense. That's a pain though. Without the cast operator it generates an error on my compiler to try to cast from an instance to a pointer. So moving the code from a compiler that does the cast incorrectly to one that does it correctly requires you either change the code using the class or leave it in place when it is unneeded.

PS: Where did you get this one at? Do you have a book that gives details like this one? If so what is it? I'm trying to go through some of the more arcance details of the language and referance material is hard to come by. I'm about to the point of just reading the standard instead. I expect that to be written for implementors though. I've been looking for Object Oriented Programming in C++ by Nicolai M. Josuttis, but have yet to find it in a store. Stroustrup had the type conversion operator, but not the pointer type conversion operator.

[edited by - LilBudyWizer on December 21, 2002 2:01:47 PM]
Keys to success: Ability, ambition and opportunity.
Fruny
Fruny
quote:

The reason for this is that the default behaviour (as far as I can determine) when casting between two pointer types is to do a direct pointer-typecast without referring to explicitly defined member typecast operators.



Kinda. The reason is simply that a pointer is not an object.
Thus a pointer has no member functions, no destructor ...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.