Garbage Collection in C++ — why?

后端 未结 16 2157
北恋
北恋 2020-11-29 18:32

I keep hearing people complaining that C++ doesn\'t have garbage collection. I also hear that the C++ Standards Committee is looking at adding it to the language. I\'m afrai

16条回答
  •  感情败类
    2020-11-29 19:11

    Garbage collection allows to postpone the decision about who owns an object.

    C++ uses value semantics, so with RAII, indeed, objects are recollected when going out of scope. This is sometimes referred to as "immediate GC".

    When your program starts using reference-semantics (through smart pointers etc...), the language does no longer support you, you're left to the wit of your smart pointer library.

    The tricky thing about GC is deciding upon when an object is no longer needed.

提交回复
热议问题