« first day (1133 days earlier)      last day (1703 days later) » 

4:15 AM
Why is it idiomatic to use & when iterating over each character in a string?
e.g.


void print(const std::string &s)
{
for (char const &c: s) {
std::cout << c << ' ';
}
}
copied from here https://www.techiedelight.com/iterate-over-characters-string-cpp/
same idiom is prevalent here stackoverflow.com/questions/9438209/…
 
 
16 hours later…
7:46 PM
Why does this code cause core dumped error? I am trying to use unique_ptr for the first time by the way. And I thought here, the 5 would be printed and when the function foo is left, the foob object's destructor would be called, thus unique_ptr's too
Also, another question, if I have some foo unique_ptr in a Foo class, and I call the Foo class' default copy constructor, would the foo unique_ptr address just be copied into the other, just as it would be with the eaw pointer? (I do not want shared_ptr)
 
 
2 hours later…
10:08 PM
@MuhamedCicak A unique_ptr takes ownership of the object it points to, and (unless you specify a deleter that does otherwise) will try to use delete/delete[] on the pointee when the unique_ptr` goes out of scope. Trying to do that to an object that wasn't allocated from the free store will do bad things.
@MuhamedCicak No. As you'll quickly find if you try it, a unique_ptr can't be copied, so the compiler won't synthesize a copy ctor for your class if it contains a unique_ptr.
A unique_ptr can, on the other hand, be moved, so (unless you do something else that prevents it) the compiler will synthesize a move ctor for you.
 
@JerryCoffin Oh I see, so I basically used a "delete" operation but not a "new" operation, so I'm deleteing a local variable that will be deleted automatically when it goes out of its scope, so its a disaster, did I get it right?
@JerryCoffin Oh I see... And what if I want to copy it, just like a raw pointer, a shallow copy, but not move it.
 
@MuhamedCicak Yes, exactly.
@MuhamedCicak You probably don't. The basic point of a unique_ptr is that it's unique--that its, there's only one pointer to the pointee object (so that pointer unambiguously owns the pointee object). Copying it would defeat that purpose, and result in a situation like the preceding, where both unique_ptrs would try to delete the same object, and bad things would happen.
 
@JerryCoffin Oh I see, I understand.
Thank you for your help!
 
10:28 PM
Howdy folks. I'm trying to follow this deciphertechnic.com/install-opencv-with-visual-studio but it keeps saying that cv is not a namespace name
 
11:24 PM
Hi! have a question but with a lot of code )
how can I paste it here? ))
would be great if somebody explain me something about c++
 

« first day (1133 days earlier)      last day (1703 days later) »