在C ++中将类变量地址初始化为NULL?

假设我有一个如下所示的类:

  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.    
       }

高级谢谢您的支持。

评论
若初見
若初見

你不能声明后,对象始终存在。

Instead you could dynamically allocate the Sigintgen when needed, and have CBase store a pointer (which can start as nullptr).

Or use std::optional.

点赞
评论