Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

Int drive()

{ return 0; }

};

}

namespace City

{

public struct CHouse

{

static int showroof()

{ return 0; }

};

}

Int main()

{

{

using namespace Family;

CHouse::protect();

}

using namespace City;

CHouse::showroof();

return 0;

}

Before creating a “physical” scope, we saw that the compiler is able to point out what problem occurred at compilation time. Fortunately, the compiler is able to explicitly designate what problem it encountered. In this case there is a conflict in name resolution: two namespaces have a member of the same name.

The solution, which is commonly used, is to qualify the variable that is causing the conflict. You can qualify only the second house call because the compiler will associate the first house call with the first using namespace. The safest way is to qualify both calls of the member, as follows:

namespace Family

{

public struct CHouse

{

static int protect()

{ return 0; }

};

public struct CCar

{

Int drive()

{ return 0; }

};

}

namespace City

{

public struct CHouse

{

static int showroof()

{ return 0; }

};

}

Int main()

{

using namespace Family;

using namespace City;

Family::CHouse::protect();

City::CHouse::showroof();

return 0;

}

1.11.5Nesting Namespaces

Nesting a namespace is the ability to include a namespace inside (as part of the body) of another namespace. To do this, create the intended namespace as a member of the parent namespace. The nested namespace should have its own name and its own body. Here is an example:

namespace Country

{

namespace City

{

public struct CHouse

{

static int showroof()

{ return 0; }

};

}

struct CCar

{

};

}

To access a member of a nested namespace, first call its parent, type the :: operator, type the name of the nested namespace, followed by the :: operator, then type the name of the variable you are trying to access. Here is an example:

namespace Country

{

namespace City

{

public struct CHouse

{

static int showroof()

{ return 0; }

};

}

struct car

{

};

}

Int main()

{

Country::City::CHouse::showroof();

return 0;

}

Following the same logic, you can have as many namespaces and many nested namespaces in your application as you desire. If you nest a namespace, you can use as many :: operators to qualify each member of the nested namespace you want.

You can also use the using namespace routine by qualifying each member inside the using namespace that “needs” its parent:

namespace Country

{

namespace City

{

public struct CHouse

{

static int showroof()

{ return 0; }

};

}

public struct car

{

};

}

Int main()

{

using namespace Country::City;

CHouse::showroof();

return 0;

}

Otherwise, you can create a using namespace for each namespace and make sure that each one of them controls its scope. As long as you are using the :: operator to identify the member that is being accessed inside of a using namespace, you can call the member of any namespace in any scope, provided you qualify it.