Can a class share a namespace's name?

前端 未结 3 1606
旧时难觅i
旧时难觅i 2020-12-16 11:50

Is the following C++ code valid?

namespace Foo
{
    class Bar
    {
        // Class code here.
    };
}

namespace Foo
{
         


        
3条回答
  •  暖寄归人
    2020-12-16 12:21

    No, but you can have SomeFunction be a static member of the Bar class.

    namespace Foo
    {
        class Bar
        {
            // Class code here.
            static void SomeFunction()
            {
                // Function code here.
            }
        };
    }
    

    The result is not 100% equivalent to what you want (because of ADL) but the qualified names are what you expect.

提交回复
热议问题