定义常量 scope_refptr() 是否有意义


scoped_refptr<any_class> a_scoped_refptr;
const scoped_refptr<any_class> something = a_scoped_refptr; // Compile error.

如果是常量,我们不能修改ref_counter,但我想暗示指针指向的内容不应该改变,我该怎么办?

两件事:

首先,您没有

提到编译器错误,但我猜这是一个简单的语法错误,因为您没有为scoped_refptr指定变量名称。

其次,如果需要指向 const 对象的指针,请将其指定为指针类型。所以试试:

scoped_refptr<any_class const> myPointer = a_scoped_refptr;

相关内容

最新更新