我知道类内的const变量必须是静态的,因为编译器无法在编译时访问它们。 但是,为什么我可以在此类中包含一个包含非静态const的非静态方法,那么在编译时也不应不可访问吗?
class ImmutablePoint {
void someNonStaticMethod(){
const v = 3;
}
}
答案 0 :(得分:0)
我知道类内的const变量必须是静态的,因为编译器无法在编译时访问它们-
编译器可以访问所需的任何内容。毕竟,它可以访问您所有的源代码。
const
成员必须是static
成员,以强调const
对象是共享的,并且不能在实例之间重复。 (It also wasn't always the case that const
class members required static
。)