遇到了不允许把 const Node* 赋值给 Node* 的情况,除强制转化外还有没有别的简洁一点的办法
蒟蒻写了一个 LCT ,里面有如下语句(蒟蒻 sb 指针写法):
struct LCT
{
struct Node{...};
static const _null = (Node){...}, *null = &_null;
void func()
{
Node *t = null;
}
}
然后报错了:
error: ‘constexpr’ needed for in-class initialization of static data member ‘const LCT::Node LCT::_null’ of non-integral type [-fpermissive]
大概就是说我这结构体里面的 static 初始化要加 constexpr ,于是我加上了 constexpr :
struct LCT
{
struct Node{...};
constexpr static const _null = (Node){...}, *null = &_null;
void func()
{
Node *t = null;
}
}
然后又报错:
error: invalid conversion from ‘const LCT::Node*’ to ‘LCT::Node*’ [-fpermissive]
就是说, Node *t = null; 这里,我把 const Node* 赋值给了 Node* ,所以我就去掉了 const
结果还是报错,我去一查,好像 constexpr 会自动给 _null 加上 const 什么的(如果错了请 dalao 指正)
这里如果我用强制转化 Node *t = (Node*)null 的化,整个 LCT 里面所有的定义都要改,而且感觉好麻烦;并且其实我比较偏向于有 const 因为 const 的调用会快一些
so ,万能的你谷巨佬们,有木有什么办法可以简洁的解决这个问题呢?还是说我只能强制转化?