C++ Object Instantiation

后端 未结 9 2006
孤街浪徒
孤街浪徒 2020-11-30 16:36

I\'m a C programmer trying to understand C++. Many tutorials demonstrate object instantiation using a snippet such as:

Dog* sparky = new Dog();
9条回答
  •  鱼传尺愫
    2020-11-30 17:04

    Well, the reason to use the pointer would be exactly the same that the reason to use pointers in C allocated with malloc: if you want your object to live longer than your variable!

    It is even highly recommended to NOT use the new operator if you can avoid it. Especially if you use exceptions. In general it is much safer to let the compiler free your objects.

提交回复
热议问题