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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -