In C++ how is function overloading typically implemented?

后端 未结 8 1904
醉酒成梦
醉酒成梦 2020-12-09 04:07

If there is no function overloading, the function name serves as the address of the function code, and when a function is being called, its address is easy to find using its

8条回答
  •  执笔经年
    2020-12-09 04:44

    If you are talking about overloaded methods of the same class, like so:

    void function(int n);
    void function(char *s);
    ...
    
    objectInstance->function("Hello World")  
    

    It is a compile time thingy. The compiler knows (or in some situations, makes a best guess) at this point which method to call.

    A comment I made in the question, I repeat here.

    People who suggest name mangling are misguided I think. It is not as if the compiler mangles the name and just does a lookup among the mangled names. It needs to infer the proper types from the available methods. Once it does that, it already knows which method to call. It then uses the mangled name as the last step. Name mangling is not a prerequisite for determining which overloaded function to call.

提交回复
热议问题