logo

Crowdly

Browser

Додати до Chrome

The following code fragment defines a namespace named Lab in a C++ source file...

✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.

The following code fragment defines a namespace named Lab in a C++ source file:

namespace Lab {

int id;

struct Vector {

int x,y,z;

};

} // notice the lack of a ; character after the closing curly brace!!!

The above code fragment resembles a C/C++ structure definition with both using curly braces to delimit scope and both declaring members, but they are quite different. Keyword struct is used to define an user-defined type that can be instantiated as an object stored in memory. On the other hand, keyword namespace is used to organize names [types, functions, variables] into logical groups to prevent name collisions. For example, namespace Lab adds scope Lab to the names id and Vector declared in that namespace. That is, the names declared in namespace Lab are distinct from similar names declared in both the global scope or in some other namespace such as namespace Lecture:

namespace Lecture {

int id; // this name will not clash with the name id declared in namespace Lab

struct Vector { // ditto

int x,y,z;

};

} // end namespace Lecture

Outside the namespace scope, members can be accessed by specifying a fully qualified name consisting of the namespace's name followed by the scope resolution operator :: and then followed by the member's name. Therefore, type Vector in namespace Lab will be accessed outside the namespace like this: Lab::Vector.

All names at namespace scope are visible to one another without qualification.

What is the output when function foo is invoked?

namespace A {

char c = 'a';

}

char c = 'b';

void foo() {

std::cout << A::c << c;

}

0%
0%
0%
0%
Більше питань подібних до цього

Хочете миттєвий доступ до всіх перевірених відповідей на distance3.sg.digipen.edu?

Отримайте необмежений доступ до відповідей на екзаменаційні питання - встановіть розширення Crowdly зараз!

Browser

Додати до Chrome