[英]Initialize a class variables address to NULL in C++?
Lets assume that i have a class as shown below:假设我有一个 class 如下所示:
class CBase
{
public:
CBase(void){};
~CBase(void){};
Sigintgen m_oSignals;// This is another class variable which i have not defined here.
};
The code begins as shown below:代码开始如下所示:
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.
}
Advanced thanks for the support.高级感谢支持。
You can't.你不能。 An object always exists once declared. object 一旦声明就始终存在。
Instead you could dynamically allocate the Sigintgen
when needed, and have CBase
store a pointer (which can start as nullptr
).相反,您可以在需要时动态分配Sigintgen
,并让CBase
存储一个指针(可以从nullptr
开始)。
Or use std::optional
.或使用std::optional
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.