Can you predict the output of the program below:
#include
Using namespace std;
Namespace n {
Void g (int) {cout << "g (int) / n";}
}
USING NAMESPACE N;
Namespace n {
Void g (char) {cout << "g (char) / n";}
}
Int main () {
g ('x');
Return 0;
}
Its output is: g (char). The key to this problem is whether the G (char) declaration is within the scope of the reference.
It is indeed within this range! Because "Using Namespace N" is brought into N named space all visual
The declaration is not only where the using command appears (also in the subsequent quotes). This is
It means that the G (char) statement is within the call range of G ('x'). Because G (Char) is more suitable than G (int),
So G (char) is called.
So, what is the result of this program?
#include
Using namespace std;
Struct C;
Struct d {
Void Operator * (d) {cout << "one / n";
} C;
Struct e {
Void Operator * (e) {cout << "two / n";
} F;
Struct f;
Int main () {
C * C;
F * f;
Return 0;
} The key to this problem is the meaning of the top 2 statements in the main () function. According to the grammar, these statements are the declarations of the pointer. Blank plays a role. In each statement, the variable (C or F) is declared as a Class, as a global object name. In this case, ignore the order of declarations, the object name hide the class name. And this class name can only use a complex statement (eg.struct c * c;) reference Every statement is only the application of Operator *. We can rewrite the following statement: int main () {
C.Operator * (c); // invokes d :: operator * (d)
F.Operator * (f); // invokes E :: Operator * (e)
Return 0;
} So our program output is: OneTwo