根据C++ Primer一节,7.4.1类型名称是特殊的:
通常,内部作用域可以从外部作用域重新定义名称,即使该名称已在内部作用域中使用过.但是,在类中,如果成员使用外部作用域中的名称并且该名称是类型,则该类可能不会随后重新定义该名称.
因此,例如:
typedef double Money;
class Account {
public:
Money balance() { return bal; }
private:
typedef double Money;
Money bal;
};
int main() {
typedef double Money;
Money asset;
typedef double Money;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当你编译上面的例子时,它会抱怨:
a.cc:6:24: error: declaration of ‘typedef double Account::Money’ [-fpermissive]
typedef double Money;
^
a.cc:1:16: error: changes meaning of ‘Money’ from ‘typedef double Money’ [-fpermissive]
typedef double Money;
Run Code Online (Sandbox Code Playgroud)
那么为什么我们不能在类中重新定义类型名称,但我们可以在内部范围内吗?
我的编译器版本是g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609.
该部分还有一个注释:
虽然重新定义类型名称是错误的,但编译器不需要诊断此错误.即使程序出错,一些编译器也会悄悄地接受这样的代码.