C++ 静态类变量——与构造函数一起使用

C++ 静态类变量——与构造函数一起使用,c++,C++,我有一个带有静态变量的类:null static Pointer<Value> Null; 该行: static Pointer<Value> Null; 该行: static Pointer<Value> Null; 我假设你的类名为Value // Header file class Value { public: ... static const Pointer<Value> Null; }; // This shou

我有一个带有静态变量的类:null

static Pointer<Value> Null;
该行:

static Pointer<Value> Null;
该行:

static Pointer<Value> Null;

我假设你的类名为Value

// Header file
class Value
{
public:
    ...
    static const Pointer<Value> Null;
};

// This should be in the cpp file.
const Pointer<Value> Value::Null(new Value);
//头文件
阶级价值
{
公众:
...
静态常量指针Null;
};
//这应该在cpp文件中。
常量指针值::Null(新值);

我假设您的类名为Value

// Header file
class Value
{
public:
    ...
    static const Pointer<Value> Null;
};

// This should be in the cpp file.
const Pointer<Value> Value::Null(new Value);
//头文件
阶级价值
{
公众:
...
静态常量指针Null;
};
//这应该在cpp文件中。
常量指针值::Null(新值);

我认为应该是这样的:

// In the header file.
class Value
{
public:
    ...
    static const Pointer<Value> Null;
};

// In the cpp file.
const Pointer<Value> Value::Null(reinterpret_cast<Value*>(0));
//在头文件中。
阶级价值
{
公众:
...
静态常量指针Null;
};
//在cpp文件中。
常量指针值::Null(重新解释强制转换(0));

我认为应该是这样的:

// In the header file.
class Value
{
public:
    ...
    static const Pointer<Value> Null;
};

// In the cpp file.
const Pointer<Value> Value::Null(reinterpret_cast<Value*>(0));
//在头文件中。
阶级价值
{
公众:
...
静态常量指针Null;
};
//在cpp文件中。
常量指针值::Null(重新解释强制转换(0));

此定义是否在源文件中的全局范围内?能否向我们展示更多代码?构造函数中发生了什么?
Null
成员属于哪个类此外,我认为可能有更好的方法来完成您想要的操作,例如Boost或Boost中的智能指针类。可选。为什么要调用变量“Null”-/这个定义在源文件的全局范围内吗?你能给我们看更多的代码吗?构造函数中发生了什么?
Null
成员属于哪个类此外,我认为可能有更好的方法来完成您想要的操作,例如Boost或Boost中的智能指针类。可选。为什么要调用变量“Null”-/这个假设不正确吗?您能用实际的空静态成员显示您的值类吗?还要确保指针模板的类定义包含在cpp文件中,并且该值本身也包含在内。假设不正确吗?你能用实际的空静态成员显示你的值类吗?还要确保指针模板的类定义包含在cpp文件中,并且该值本身也包含在内。看看你现在发布的代码,问题是你将构造函数声明为显式的。这意味着您必须删除
explicit
关键字,或者使用
重新解释\u cast
或为
void*
创建新的显式受保护构造函数。查看您现在发布的代码,问题是您将构造函数声明为
explicit
。这意味着您必须删除
explicit
关键字,或者使用
重新解释\u cast
或为该情况创建一个新的显式受保护构造函数。
// In the header file.
class Value
{
public:
    ...
    static const Pointer<Value> Null;
};

// In the cpp file.
const Pointer<Value> Value::Null(reinterpret_cast<Value*>(0));