Wouldn't that just fail to compile, since the const version hides the non-const version, and the const version can't be called with the non-const pointer?Quote:
Only then the const version is called
Printable View
Wouldn't that just fail to compile, since the const version hides the non-const version, and the const version can't be called with the non-const pointer?Quote:
Only then the const version is called
The cast takes care of the not compiling part. Of course its VERY crappy code when you do that, it was just an example. If the memory of the object actually resides in read-only memory and the non const version changes members you have yourself a nice crashing program ;)
Not really. The cast casts to a non-const type, and you try to call a const function on it. It should just fail to compile, and if it doesn't, you've got a really weird compiler.Quote:
The cast takes care of the not compiling part.
1. according to your design handleEvents( EventHandler *eh ) seems to take a polymorphic argument and you will invoke the handleEvent() method on that EventHandler* or EventHandler* Derived Object. So making handleEvent private in a derived Class vreaks your own design.
2. when you are casting (void)e you are not changing type of MyEventHandler::e to void rather you are either returning a copy of e which holds the same data of e but type void (which happens here), or returning the value of e in void format.
Other than reiterating various known facts and merging them with meaningless ranting, the initial question was "why?" not "what?"
Did you put the excessively-whiny-about-everything-because-i-cant-avoid-not-designing-my-classes-poorly flag? I think its a gcc thing.
I always -Wall myself ;)
At this point I guess, although the standard is a bit flaky in this regard, it's not a flaw in the language so much as in compiler design. Just another thing to add to the mailing list...
Fair enough. I am sure Stroustrup will send you back a personally autographed punch card from the first IBM mainframe he ever programmed.