tom@tnosoes.UUCP (Tom Vijlbrief) (01/31/89)
diamond@csl.sony.JUNET (Norman Diamond) writes: >In article <435@tnosoes.UUCP>, tom@tnosoes.UUCP (Tom Vijlbrief) writes: >> When compiling a file of the OOPS package, G++ (1.32) produced an >> errormessage for the following function: >> ((Date)*this).printOn(strm); // Error ! >When you cast the result of *this, it is like assigning the >result of *this to a temporary variable of type Date. When >you give a command to a temporary variable and then have no >further use for that variable, you don't accomplish much. This is not true. What is accomplished is that function Date::printOn is executed for a temporay Date which is produced by converting a Time object to a Date object. THIS IS WHAT IS INTENDED. Instances of Time (*this) and Date have different virtual functions printOn. >> ((Date *)this)->printOn(strm); // No Error ! >Aha. When you have sort of a temporary variable of type >(Date *), it still points to your own instance. You then >command your own instance to do something, and you get >what you want. This statement does not produce the errormessage, but the result is not what was intended. The type conversion from Time to Date is not done. This is a barebone copy of the error producing code: ============================== class time; class date { date(date&); public: virtual void dummy() {}; }; class time { operator date(); void f(); }; void time::f() { ((date)*this).dummy(); // conv.c:17: both constructor and type conversion operator apply }