5 ms·
auto *p = new T; What is the lifetime of the object to which p points? The trouble with defining the reference-containing struct as a value type is that assig
by MathMonkeyMan 3y ago
auto *p = new T;
What is the lifetime of the object to which p points?
The trouble with defining the reference-containing struct as a value type is that assignment to the struct assigns _through_ the reference rather than making it refer to something else.
My impression of the article is that more precise and pedantic language is required to talk about these things.
- drtgh 3y agoif one is using auto, then contemporary C++ is being used, and I think smart pointers are expected to be used, the article is mixing classical and contemporary features. So IMHO, the problem is such features consumption approach, if one is using raw pointers for any reason, all that area and subsequent should use classical structures, and use some kind of interfaces for to connect them to contemporary structures (suboptimal thought?), never mixing, case contrary to debug those things will be epic poetry (oh, the code uses auto, but is taking care of the pointers or no, damn, I have to read all the library for to know).
- jebarker 3y ago> the article is mixing classical and contemporary features As a C++ novice that is practicing to improve this is the number one problem with finding tutorials. It's really hard for me to figure out which features are current best practice and which shouldn't be used together. I can obviously dig in to specific topics to read the history and pros/cons of features, but that's a very slow way to learn.
- zabzonk 3y agoplease note that my comment said: > the lifetime of a named object in this case "new T" does not create a named object. the lifetime of an unnamed object is determined explicitly by the programmer (i.e. "delete p" in this case), or implicitly if the object is being pointed to by something like a smart pointer, or is undefined (if you didn't assign the result of calling "new" to anything).
- MathMonkeyMan 3y agoah, point taken. Reference data members are still tricky, though.
- zabzonk 3y agoreference data members are generaly A Bad Thing (your design is probably wrong) - the main purpose of references is to serve as function parameters and/or return types. if you need a reference-like member, use a pointer.
- wiseowise 3y agoUntil delete is called on it.