开发者

Static class variable--use with constructors

开发者 https://www.devze.com 2023-01-04 19:27 出处:网络
I have a class with a static variable: null. static Pointer<Value> Null; Pointer is a class which uses reference count memory management.

I have a class with a static variable: null.

static Pointer<Value> Null;

Pointer is a class which uses reference count memory management.

However, I get an error: no matching function for call to Pointer::Pointer()

On the line:

Pointer<Value> Value::Null(new Value());

Thanks.

Excerpt of Pointer class:

template <typename T>
class Pointer
{
 public:
  explicit Pointer(T* inPtr);

Constructor Source

  mPtr = inPtr;
  if (sRefCountMap.find(mPtr) == sRefCountMap.e开发者_运维百科nd()) {  
    sRefCountMap[mPtr] = 1;
  } else {
    sRefCountMap[mPtr]++;
  }


The line:

static Pointer<Value> Null;

Is call the Pointer::Pointer() constructor. It apears that your Pointer class does not have such a constructor, but instead has a constructor that accepts void*. So try changing it to

static Pointer<Value> Null(0);


I'm assuming your class is named 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);


I believe it should be like:

// 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));
0

精彩评论

暂无评论...
验证码 换一张
取 消