C++ Virtues of a virtual override

For this reason, I'm in favor of ABSOLUTELY NO AUTOCONVERSION. In my opinion, if someone says "int pi_rounded = 3.14159", that should give a compile error.

Use braced initialisation and the compiler will produce an error.

int pi_rounded{3.14159};

<source>:1:16: error: narrowing conversion of '3.1415899999999999e+0' from 'double' to 'int' [-Wnarrowing]
1 | int pi_rounded{3.14159};
| ^~~~~~~
Compiler returned: 1

Implicit type conversions was one of the stupidest things that C++ needed from C to remain highly compatible.
 
You can't now do:
Code:
Manager m;
m.jump(9.0f);

Weird huh? When a derived class declares a function with the same name as a base class function, it hides all base class overloads with that name, not just the one with the same signature.

That's just how name lookup works. You have a set of scopes from the current scope out to the global scope where each scope is a namespace or a level of inheritance. Name lookup will generate a set of candidates for the current scope. If there are none it moves out to the next scope, repeating until it gets to the global scope. As soon as it has a viable candidate it stops. That is how a "worse" candidate at an inner scope will hide a "better" candidate at an outer scope (where better/worse mean fewer/more argument conversions). If there are no viable candidates at the global scope then you get an error.
 
Back
Top