namespaces, classes and free functions - when do you need fully qualified names

前端 未结 4 1843
醉梦人生
醉梦人生 2021-02-03 13:30

In my example below, why do I have to fully qualify the name of the free function in the cpp to avoid linker errors and why does it work for the class function without? Can you

4条回答
  •  醉梦人生
    2021-02-03 14:08

    If you don't qualify FreeFunction definition, the compiler does not know for sure anther you want to provide implementation for the previously forward-declared Test::FreeFunction or for a separate FreeFunction in the current namespace.

    On the other hand, there's only one way to resolve the name CTest - as the class definition from the Test namespace. Thus, there's no need to fully qualify it.

    However, if the CTest name resolution is ambiguous (say there's another CTest class in the current namespace as well), you will have to fully qualify the method declarations.

提交回复
热议问题