我知道我们不能static
在本地类中声明成员变量......但原因尚不清楚。
所以请任何人都可以解释一下吗?
另外,为什么我们不能static
直接在局部类成员函数中访问定义在函数内部的非变量,在该函数中定义了局部类?
在下面给出的代码中:
int main(int argc, char *argv[])
{
static size_t staticValue = 0;
class Local
{
int d_argc; // non-static data members OK
public:
enum // enums OK
{
value = 5
};
Local(int argc) // constructors and member functions OK
: // in-class implementation required
d_argc(argc)
{
// global data: accessible
cout << "Local constructor\n";
// static function variables: accessible
staticValue += 5;
}
static void hello() // static member functions: OK
{
cout << "hello world\n";
}
};
Local::hello(); // call Local static member
Local loc(argc); // define object of a local class.
return 0;
}
静态变量staticValue
可以直接访问,而另一方面,argc
参数 frommain
不是....