现在的位置: 首页 > 综合 > 正文

static作用域造成一则小问题

2018年02月12日 ⁄ 综合 ⁄ 共 318字 ⁄ 字号 评论关闭

成员函数的作用域是类域, 而在类体外加上static不是表示静态函数,表示的是函数拥有文件域(file scope)
而类域是小于文件域,强行把类域扩大到文件域,就会出错。
如下代码:
class CA {
public:
static void display(void);
};

static void CA::display(void) { // ERROR!
cout < < "Hello CA!" < < endl;
}

int main(int argc, char* argv[]) {
CA::display();
}

// error C2724: 'CA::display' : 'static' should not be used on member functions defined at file scope

【上篇】
【下篇】

抱歉!评论已关闭.