在C++中初始化类变量地址为null?
假设我有一个类,如下所示:在C++中初始化类变量地址为null?,c++,class,C++,Class,假设我有一个类,如下所示: class CBase { public: CBase(void){}; ~CBase(void){}; Sigintgen m_oSignals;// This is another class variable which i have not defined here. }; main() { CBasec *ptr = new C
class CBase
{
public:
CBase(void){};
~CBase(void){};
Sigintgen m_oSignals;// This is another class variable which i have not defined here.
};
main()
{
CBasec *ptr = new CBasec();
I would want the following to happen as shown below:
&(ptr->m_oSignals) should be equal to NULL. Can anyone please suggest me how to get this?
&(ptr->m_oSignals) = NULL; //This was tried but compilation errors.
}
代码开始如下所示:
class CBase
{
public:
CBase(void){};
~CBase(void){};
Sigintgen m_oSignals;// This is another class variable which i have not defined here.
};
main()
{
CBasec *ptr = new CBasec();
I would want the following to happen as shown below:
&(ptr->m_oSignals) should be equal to NULL. Can anyone please suggest me how to get this?
&(ptr->m_oSignals) = NULL; //This was tried but compilation errors.
}
高级感谢您的支持。您不能。对象在声明后始终存在 相反,您可以在需要时动态分配Sigintgen,并让CBase存储一个指针,该指针可以以nullptr开头
或者使用std::optional。一般来说,如果使用std::shared\u ptr而不是*,会更好,它将帮助您解决内存管理问题,并可能减少使用指针的恐惧,我假设您这样做是因为您没有将其用于Sigintgen m\u oSignals。成员变量的地址不能为空。让成员变量的地址等于NULL,您想实现什么?@PeterG。我之所以需要它来使用google测试框架进行单元测试