c++ - Why would the compiler use a virtual call for `datum::rotate()` below? -


on page 26 of "inside c++ object module" s. lippman, you'll find following snippet:

void rotate( x datum, const x *pointer, const x &reference ) {     // cannot determine until run-time     // actual instance of rotate() invoked     (*pointer).rotate();     reference.rotate();     // invokes x::rotate()     datum.rotate(); } main() {     z z; // subtype of x     rotate( z, &z, z );     return 0; } 

and paragraph:

the 2 invocations through pointer , reference resolved dynamically. in example, both invoke z::rotate(). invocation through datum may or may not invoked through virtual mechanism; however, invoke x::rotate().

afaik, datum.rotate() invoked static call. why compiler use virtual call here?

the compiler may choose use virtual call though doing not necessary. standard not compiler "must" convert static call. long "right" function called, compiler has done it's job.

edit: appear standard (at least n3337) doesn't how compiler supposed called - says

note: interpretation of call of virtual function depends on type of object called (the dynamic type), whereas interpretation of call of non-virtual member function depends on type of pointer or reference denoting object (the static type) (5.2.2). — end note


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -