Strange memory problems

Started by
12 comments, last by rick_appleton 20 years, 1 month ago
Wow. Be my mentor?

Seriously. Does that render Sneftel''s Compile-Time Polymorphism as undefined behavior then? He reinterpret_casts from a base to a derived using the curiously recurring template idiom.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Advertisement
Technically, yes, it's dependent on the compiler, undefined behaviour and all that. However, I don't know of any compiler that will reject his code (as it appears in the article anyways. It will probably break in the presence of multiple inheritence). But, in the "Discuss this article" thread you'll see people talking about needing to use static_cast<> instead. Sneftel even admits to that being the correct method.

edit: and you must be getting desparate if you'd want someone as foul-tempered as me to be your mentor.

[edited by - SiCrane on March 5, 2004 5:53:22 PM]
Thanks to all who answered, but Clash gets the cookie. While I totally overlooked the virtual destructor in the base class, there was actually another error which made destroying a CModel object impossible. I was deleting name and pMaterials without checking if they had been set.

I added in the virtual destructor (which really should be there of course), and it all worked perfectly. Even the addresse shifts where gone. I commented out the virtual destructor, and the shifts where back, I uncommented it, and the shifts where gone again. So somehow the existence of the virtual destructor enables correct casting.

Thank you all, you''ve made my night.
quote:Original post by SiCrane
edit: and you must be getting desparate if you''d want someone as foul-tempered as me to be your mentor.

Just take it as a compliment.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement