Using a class in a namespace with the same name?

前端 未结 4 980
耶瑟儿~
耶瑟儿~ 2021-01-11 15:11

I have to use an API provided by a DLL with a header like this

namespace ALongNameToType {
    class ALongNameToType {
        static void Foo();   
    }
}
         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 15:32

    There are three ways to use using. One is for an entire namespace, one is for particular things in a namespace, and one is for a derived class saying it doesn't want to hide something declared/defined in a base class. You can use the second of those:

    using ALongNameToType::ALongNameToType
    

    Unfortunately this isn't working for you (due to the ambiguity of the namespace and the class having the same name). Combining this type of using with a previous answer should get rid of the ambiguity:

    namespace alntt = ALongNameToType;
    using alntt::ALongNameToType;
    

    But once you've renamed the namespace, you really don't need the using statement (as long as you're comfortable writing the (shortened) namespace every time you use the class:

    namespace alntt = ALongNameToType;
    alntt::ALongNameToType a;
    ...
    

提交回复
热议问题