1

I have been reading about "Name resolution" in wikipedia (Name resolution WIKI) and it has been given in that that C++ uses "Static Name Resolution". If that is true then I couldn't figure out how C++ manages to provide "polymorphism" without using dynamic name resolution.

Can anyone please answer whether C++ uses "Static Name Resolution" or "Dynamic Name Resolution". If it is static, can you also explain how C++ provides polymorphism.

pasha
  • 2,035
  • 20
  • 34
  • 1
    everything that is says dynamic polymorphism in c++ is related with keyword "virtual". Read more about it. – rahul.deshmukhpatil Dec 15 '15 at 18:48
  • It uses both. When you use abstract class use dynamic. For other things like templates, overloading, etc. it uses static. – Edwin Rodríguez Dec 15 '15 at 18:50
  • 2
    @EdwinRodríguez I don't think *name resolution* is dynamic when you use an abstract class. Names are resolved at compile time. – juanchopanza Dec 15 '15 at 18:58
  • To clear up, can anyone point out the mistake in below paragraph. C and D are classes derived from B. B has a virtual function F and C, D overridden F in their implementations. x is a pointer to B and within my application x->F may be F of B,C or D depending upon which object I've made x point to, which I can do depending on user input at runtime. If C++ uses static name resolution then x->F must be associated with only one entity i.e., either with F of B, C or D at compile time and should not change at runtime. – pasha Dec 15 '15 at 19:13
  • 1
    "If C++ uses static name resolution then x->F must be associated with only one entity i.e., either with F of B, C or D " No. This: "If C++ uses static name resolution then and there is some code `x->F` then `B::F` must exist at compile time (but can be virtual)." Dynamic name resolution is 100% unrelated to dynamic dispatch (virtual functions) – Mooing Duck Dec 15 '15 at 19:22
  • I missed a part: `B::F` must exist at compile time _and cannot be changed while running_. (For virtual functions, the compiler generates a function with some dispatch code, which meets these requirements, it's not _actually_ replacing the function's name) – Mooing Duck Dec 15 '15 at 19:29

2 Answers2

7

Wikipedia's definition of name resolution is about how tokens are resolved into the names of constructs (functions, typenames, etc). Given that definition, C++ is 100% static with its name resolution. Every token that represents an identifier must be associated at compile-time with a specific entity.

C++ polymorphism is effectively cheating. The compiler can see that a static name resolves to a member function defined with the virtual keyword. If the compiler sees that the object you are calling this on is a dynamic object (ie: a pointer/reference to that type rather than a value of that type), the the compiler emits special code to call that function.

This special code does not change the name it resolves to. What it changes is the function that eventually gets called. That is not dynamic naming; that is dynamic function dispatch. The name gets resolved at compile-time; the function gets resolved at runtime.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

C++ use static name resolution because it renames each function to made each one have an unique.
That mean that the function int foo(int bar) will be known by the compiler as something like _Z3fooi, while int foo(float bar) will be known as something like _Z3foof.
This is what we call name mangling.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Emeraude
  • 9
  • 2