开发者

what should i use with a c++ class, with or without "new"?

When should i use the pointer and when not? Whats the better Way? Must i delete the cat object of the pointer at the end of the programm?开发者_如何学Go

Example:

cat piki;
piki.miao();
cat *piki2 = new cat();
piki2->miao();


Whenever possible try to avoid creating the object using new (i.e. on heap) as you would have to do memory management yourself (or at least you need to use smart pointers). If you allocate the object on stack (i.e. cat piki;) the memory allocated for the cat object is automatically released when the piki goes out of scope. This doesn't happen with piki2, you need to explictly do delete piki2; to release memory.


Creating an ojbect with or without a new depends on where you are using the object. An object of your class can be created without a new if you are using it within a function, or a block so that the destructor automatically gets called once your object is out of scope.

If you want your object to be used in multiple methods or you want to return the object from a function, then you need to create the object using new, and also make sure you delete it properly.


To answer "Must i delete the cat object of the pointer at the end of the programm?" which I haven't seen in other answers:
When the program ends, technically speaking, you often need not (see also remark from Tony) delete objects you allocated with new, because most os'es will clean up after you. (Notice the "most", MsDos (I don't know which versions) was an example were that didn't happen, so you had to reboot when running these programs).
However, it is considered bad practice not to delete what you have newed, because programs tend to grow and what was "acceptable" before might suddenly lead to a memory leak, possibly resulting in an unstable system.

examples where deleting may not be done (I am not sggesting that in these cases you shouldn't care):

  • when using Singletons, to avoid dependency issues in destructors.
  • in multi-threaded systems, some threads holding any resources may be halted without cleaning up.

In simple cases of course, prefer allocating without new as suggested in other answers.


The piki object will be autodeleted when the current scope is closed.

piki2 must be explicitly deleted.

delete piki2;


This is purely specific to your requirement. One reason to use objects directly would be if you know how many objects you need. However if you are not sure how many objects you will create you can create objects using new operator each time one is required to be created.

But in case you you use a pointer like you have done

cat *piki2 - new cat();

Then as you have mentioned you need to delete the pointer once its use is over.

delete piki2;


cat *piki2 = new cat();
piki2->miao();

This is several forms of fail. For example, what if miao throws an exception? You should always use a self-releasing pointer for resources. Objects created with operator new are quite slow to create and slow to access and manage, plus additional memory overhead, so you really should use the first form wherever possible.


Use smart pointers where you can. Use boost smart pointers library.

http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/smart_ptr.htm


As Developer Art has said you could write a book about this topic. Especially if you make lists of heuristics as is sort of asked for.

However, a few rules will go a long way.

A factor to consider is that stack allocated instances of cat will not be treated polymorphically. For an object to be treeated polymorphically it must be represented by a pointer or a reference.

Any object that is no longer needed should be freed ASAP, or set on the trail of automatic deletion ( auto_ptr or some sort of reference counted smart_ptr ). Keep in mind that the memory is not just freed, the objects destructor is called -- and there is RIAA.

You also have to keep in mind that if an object is to be passed around a lot, and has an expensive copy constructor then using an object rather then a pointer is going to invoke a large cost.

Finally you need to ask yourself how long an object has to live. If it needs to live beyond the local scope then it will most likely need to be a pointer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜