Barnack
Barnack

Reputation: 941

Declare and define inside namespace - Not sure if it works because it's correct or by accident

straight to the point i have a .h file as follows

namespace ns
    {
    namespace n1
        {
        class a
            {funct();}
        class b
            {funct();}
        }
    namespace n2
        {
        class a
            {funct();}
        class b
            {funct();}
        }
    }

normally i would define the function in the .cpp file in this way:

int ns::n1::a::funct(){return 1;}
int ns::n1::b::funct(){return 1;}
int ns::n2::a::funct(){return 1;}
int ns::n2::b::funct(){return 1;}

Today i noticed this works as well as .cpp file

namespace ns
    {
    namespace n1
        {
        a::funct(){return 1;}
        b::funct(){return 1;}
        }
    namespace n2
        {
        a::funct(){return 1;}
        b::funct(){return 1;}
        }
    }

Which seems very helpful when organizing a .cpp file that has a tree of namespaces.

Is this standard or it just works by accident?

Upvotes: 0

Views: 40

Answers (1)

R Sahu
R Sahu

Reputation: 206577

Is this standard or it just works by accident?

It is correct, not the result of an accident. IMO it's also the better approach. I think it is better since you don't have to repeat the namespaces.

Upvotes: 1

Related Questions