我想更好地理解如何在存在PIMPL惯用语的情况下使用静态字段的方法。 考虑下面的代码。C++ pimpl习语和静态方法和字段
MyClass.h文件:
#ifndef MYCLASS
#define MYCLASS
class MyClass{
public:
void method();
static void static_method();
private:
Type field;
static Type *static_field;
}
#endif
MyClass.cpp文件:
#include MyClass.h
void MyClass::method(){
/*OK method definition*/
field = new Type(); /*OK field construction*/
}
void MyClass::static_method(){
/*NOT WORKING method declaraion */
static_field = new Type(); /*not working */
}
我有这2个错误:
- 不能声明成员函数static_method有一个静态链接
- static_field未在此范围内声明
我对pimpl习语不太熟悉。
所以我的问题是我如何实现静态方法和字段声明尊重PIMPL习语并成功编译它?
我在做什么错在这段代码?
我该如何更改我的代码?
'field'和'static_field'必须是'Type'的指针,而不是'Type'类型。 – Dialecticus 2011-03-28 10:39:44
@Dialecticus:是另一个错误。这是一个指针,在我的实际实现 – Heisenbug 2011-03-28 10:41:36