C++: What does the class destructor do? -


c++: class destructor do?

suppose have object "myobject", , has several members follows:

int a; float b; yourclass yourobject; void hismethod(); 

from read, memory allocated "myobject" order.

once destructor called, happened?

after destructor called, before object destroyed, read, can still access (a) object "myobject". (b) member yourobject (c) member hismethod()

can still access members? undefined behavior?

many c++ books not talk more details on it. can find more details on it? because details can me understand many c++ rules "not manually call destructor unless after placement new".

[update 1] raise question because saw post: what empty destructor do? poster gives example below:

#include <iostream> #include <set>   class { public: std::set <int> myset; };  int main() { object; object.myset.insert(55); object.~a(); object.myset.insert(20); std::cout << object.myset.size(); } 

the poster get: "* glibc detected * /.app: double free or corruption (fasttop):" , "abort".

this means:

object.myset.insert(20); 

doesn't raise error, means object still exists after destructor manually called. class member can still called!

double calling of deconstructor gives error.

[update 1] run code in qt creator, , when run object.myset.insert(20);

it raise error:

read access violation at: 0x0, flags=0x0.

in case of yourclass yourobject;, can´t access yourobject after destruction,
because compiler complain before execution.
in case (automatic storage duration), scope lifetime of object, after destruction, you´re in code part name yourobject isn´t known anymore.

(the destructor , actual memory freeing are separate, both occur consecutively without possibilty put own code between them).

if have pointer, allocated new, , delete it:
yes, it´s undefined behaviour access after delete.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -