Inheritance and namespaces

I am trying to create my first useful object-oriented program using a namespace. I have a base class B that is in the NS namespace. If I try to inherit this base class in order to get the inheritance work, I have to use NS :: B in the class declaration, as shown below, is this really so? Or is there a more widespread siat for this inheritance syntax?

namespace NS
{
    class D: public NS::B{
    ...
    };
}

Best, Umut

+5
source share
3 answers

D namespace NS, NS::B, D B . class D : public B.

+7

NS, (1) , , NS. :

namespace NS {
    class B { };
}

D :

namespace NS {
    class D : public NS::B { };
}

namespace NS {
    class D : public B { };
}

(1) - (ADL) , , .

+7

NS,

namespace NS
{
    class D: public B{ //is fine
    };
}

- :

#include <iostream>
int x = 10;

int main()
{
    int x = 5;
    //This will show the local x
    std::cout << x << std::endl;
    //This is how you access the Global x
    std::cout << ::x << std::endl;

    return 0;
}
+1
source

All Articles