Home  >  Q&A  >  body text

java - 是否类 类型指针、引用作为形参 ,函数结束不会自动析构类?

自动析构是在作用域结束时析构作用域里创建的类对象的吗?

PHP中文网PHP中文网2712 days ago760

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:53:17

    If the space pointed to by a pointernew出来的对象, 必须进行手动delete. 析构函数不会帮你自动析构, 比如std::string* s = new std::string;. 如果这是在一个类里面构造的string, 这个类会将s回收, 但是不会将s is recycled. You only need to remember that the reference is actually an alias and you can make your own judgment.

    reply
    0
  • 阿神

    阿神2017-04-18 10:53:17

    I shouldn’t have talked about so much, and I didn’t understand it right.

    At the end of the function, only the automatic variables declared in the function body and the formal parameters of the function will be destroyed (destroyed), and the objects they refer to (if any) will not be destroyed with their destruction. The object referenced by a pointer/reference has its own independent storage period. When this object is destroyed depends on its own storage period.

    What you asked in your question is when the destructor is called implicitly. The implicit call to the destructor also depends on the storage duration of the object. Simply put, if an object is constructed, its destructor will be called when it is destroyed.

    12.3.2.11 Destructors are invoked implicitly
    — for constructed objects with static storage duration (3.7.1) at program termination (3.6.3),
    — for constructed objects with thread storage duration (3.7.2) at thread exit,
    — for constructed objects with automatic storage duration (3.7.3) when the block in which an object is created exits (6.7),
    — for constructed temporary objects when the lifetime of a temporary object ends (12.2),
    — for constructed objects allocated by a new-expression (5.3.4), through use of a delete-expression (5.3.5),
    — in several situations due to the handling of exceptions (15.3).


    About Quote:

    Reference is not an object, but it also has storage period (storage period applies to any variable). The destruction rules for storage periods also apply to references.
    But I didn't find an accurate description of what happens when the reference is destroyed. Exactly how references are destroyed should depend on the compiler implementation. The general situation should be: if the reference occupies storage space during implementation, the space will be reclaimed. Without possession, nothing happens. (Formal parameters of reference types often occupy storage space when the function is not inlined)

    3.7.3 The storage duration categories apply to references as well. The lifetime of a reference is its storage duration.

    8.3.2.4 It is unspecified whether or not a reference requires storage.

    3.9.8 An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.

    1.8 [...] An object is a region of storage. [ Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. — end note ] [...]

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:53:17

    1. The destructor is introduced by C++ for classes. It is a function that is called after the life cycle of the class variable ends and before the space is recycled.
    2. Class pointers and class references (constant pointers pointing to variables) are only basic data types (pointers). There is no destructor. Their corresponding stack space will be recycled after the function call is completed.
    3. If the parameter passed is a class object, as mentioned in the first point, the destructor will be called before the space is recycled.
    4. All class variables on the stack will be automatically destructed after the life cycle ends, but class variables on the heap (allocated by operations such as new) will not and need to be manually released to trigger the call of the destructor.

    reply
    0
  • Cancelreply