开发者

Namespace nesting in C++/CLI

开发者 https://www.devze.com 2023-03-12 22:55 出处:网络
I know nesting of namespaces is allowed in C++/CLI. So my question is if we have something like this:

I know nesting of namespaces is allowed in C++/CLI. So my question is if we have something like this:

...
namespace one
{
    // blah blah blah
    // ...

    namespace two
    {
        // another set of blah blah blah
        // ...
    }
}

and I need to use some functions and variables in namespace two, do I use:

one::two

or

one.two

Another question is that if I have a statement like

using namespace one;

do I have access to variables and functions in any nested namespaces like

namespace two

I'm asking because, in some programs I have seen (and written), there's something like:

using namespace System;
using namespace System::Text;
using namespace System::IO;

Isn't the System namespace supposed to cover the Sy开发者_运维问答stem::Text and System::IO namespaces?


You need to use

one::two::some_variable_or_function

The . operator is for accessing non-static struct/class members. Static members can then again be accessed through the scope resolution operator ::.

If you only would use using System;, you could access the System::Text functions/variables by

Text::some_function_or_variable

using the parent namespace does not imply importing all sub-namespaces.


  1. Yes, you need use one::two instead of one.two to access symbols in a nested namespace.

  2. If you just using namespace one; you don't automatically have the access to the nested namespace. You'll have to use two::

Example:

namespace one
{
    int i;

    namespace two
    {
        int j;
    }
}

If you use:

using namespace one;

Your code looks like:

i = 1;
two::j = 2;

If you use:

using namespace one;
using namespace one::two;

Your code looks like:

i = 1; //Compile fails if no "using namespace one".
j = 2;


one::two

or

one.two

You need to use one::two. one.two is the syntax for package access in Java

Another question is that if I have a statement like

using namespace one;

do I have access to variables and functions in any nested namespaces like

namespace two

No, you can use them as two:: instead of one::two::

I'm asking because, in some programs I have seen (and written), there's something like:

using namespace System;
using namespace System::Text;
using namespace System::IO;

Isn't the System namespace supposed to cover the System::Text and System::IO namespaces?

No, they aren't the same. You have to specify usage of each child namespace.


In general, you use the dot only when you're referring to a member of an instance, and :: everywhere else. So to access stuff in your namespace two, you'd call it one::two::whatever.

As for using namespace System;, it'd import stuff from the System namespace. While System::IO and System::Text are in System, it doesn't import them directly into the current namespace. AFAIK you'd be able to say using namespace System; and then refer to a class within a nested namespace as, say, IO::Stream. But that would get confusing fast, if you use a bunch of namespaces.


1) one::two
2) using a parent namespace doesn't automatically expand any nested namespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消