C++ pointer to class

后端 未结 6 1719
小鲜肉
小鲜肉 2021-01-30 23:11

Can anyone tell me what the difference is between:

Display *disp = new Display();

and

Display *disp;
disp = new Display();
         


        
6条回答
  •  天命终不由人
    2021-01-31 00:04

    1) Instance of GzDisplay is created in 2nd variant, whereas in 1st variant, created instance is of Display type (I assume GzDisplay is subclass of Display, right?); 2) Besides that 1st is shorter, in 2nd disp have undefined value until assigned with new GzDisplay(). 2nd variant gives you a chance to accidentally forget that and insert some code that uses disp before it initialized.

提交回复
热议问题