3 ms·
i would like to see an example of how the c++ compiler destructs an object before you use it. the only reason for this to happen that i can imagine is not under
by zabzonk 3y ago
i would like to see an example of how the c++ compiler destructs an object before you use it. the only reason for this to happen that i can imagine is not understanding the concept of scope.
- keithalewis 3y ago{ std::string s; }
- zabzonk 3y agosee "scope" in my original comment
- foldr 3y agoThis could happen any time you return a pointer to a variable with automatic storage and then later attempt to dereference the pointer and do something with the value. Of course, you shouldn't do that, but the C++ compiler won't stop you. 'Scope' can have various meanings, but merely understanding how lexical scoping of variables works is not sufficient to understand why you shouldn't do that.
- bradrn 3y agoAs I recall, the issue in my case involved a default copy-constructor which destructed an object wrongly. I’ve since learnt to delete the implicit methods of a class, but I still find reasoning about copy and move semantics to be thoroughly puzzling.
- zabzonk 3y agoi would still think this is a misunderstanding of scope. and you really don't want to be worrying about move semantics unless you are a performance maniac, a library writer, or (most likely) both.