是。
C++11
3.4.4 Elaborated type specifiers
If the elaborated-type-specifier has no nested-name-specifier, and unless the elaborated-type-specifier appears
in a declaration with the following form:
class-key attribute-specifier-seqopt identifier ;
the identifier is looked up according to 3.4.1 but ignoring any non-type names that have been declared. If
the elaborated-type-specifier is introduced by the enum keyword and this lookup does not find a previously
declared type-name, the elaborated-type-specifier is ill-formed. If the elaborated-type-specifier is introduced by
the class-key and this lookup does not find a previously declared type-name, or if the elaborated-type-specifier
appears in a declaration with the form:
class-key attribute-specifier-seqopt identifier ;
the elaborated-type-specifier is a declaration that introduces the class-name as described in 3.3.2.
class student {
};
void func() {
int student; // 定义变量,隐藏了全句的类名
student t; // 错误,student 是一个变量
class student;
student t;
}
这样会怎么样[/quote]
C++11 3.3.10 Name Hiding
2 A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member,
function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data
member, function, or enumerator are declared in the same scope (in any order) with the same name, the
class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is
visible.
class student {
};
void func() {
int student; // 定义变量,隐藏了全句的类名
// student t; // 错误,student 是一个变量
class student; // 这是一个声明,声明了一个 func() 内部的 class student; ,不是全局的那个
student t; // 基于以上的 name hiding rule ,student 还是只能找到 int student ,于是错误。
/// 加一个:
class student t; // 这里可以找到 func() 内部声明的 class student ,但这个 class student 是一个 incomplete type (没有定义),不能用于定义变量,于是错误。
}[/quote]class student *p; 会同时完成声明 class student; 的工作,是标准中的固有的语法特性吗[/quote]
class student {
};
void func() {
int student; // 定义变量,隐藏了全句的类名
student t; // 错误,student 是一个变量
class student;
student t;
}
这样会怎么样[/quote]
C++11 3.3.10 Name Hiding
2 A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member,
function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data
member, function, or enumerator are declared in the same scope (in any order) with the same name, the
class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is
visible.
class student {
};
void func() {
int student; // 定义变量,隐藏了全句的类名
// student t; // 错误,student 是一个变量
class student; // 这是一个声明,声明了一个 func() 内部的 class student; ,不是全局的那个
student t; // 基于以上的 name hiding rule ,student 还是只能找到 int student ,于是错误。
/// 加一个:
class student t; // 这里可以找到 func() 内部声明的 class student ,但这个 class student 是一个 incomplete type (没有定义),不能用于定义变量,于是错误。
}[/quote]class student *p; 会同时完成声明 class student; 的工作,是标准中的固有的语法特性吗
class student {
};
void func() {
int student; // 定义变量,隐藏了全句的类名
student t; // 错误,student 是一个变量
class student;
student t;
}
这样会怎么样[/quote]
C++11 3.3.10 Name Hiding
2 A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member,
function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data
member, function, or enumerator are declared in the same scope (in any order) with the same name, the
class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is
visible.
class student {
};
void func() {
int student; // 定义变量,隐藏了全句的类名
// student t; // 错误,student 是一个变量
class student; // 这是一个声明,声明了一个 func() 内部的 class student; ,不是全局的那个
student t; // 基于以上的 name hiding rule ,student 还是只能找到 int student ,于是错误。
/// 加一个:
class student t; // 这里可以找到 func() 内部声明的 class student ,但这个 class student 是一个 incomplete type (没有定义),不能用于定义变量,于是错误。
}
1) class student *p; 会同时完成声明 class student; 的工作
2) class student 可以引用被隐藏的类名:
class student {
};
void func() {
int student; // 定义变量,隐藏了全句的类名
student t; // 错误,student 是一个变量
class student t; // OK
}