C++ beginner thread

Switching form C# to C++ out of curiosity.

What else did I miss out on that makes programming in C++ less of a nut job?

Attached: pointer.png (670x435, 150K)

Other urls found in this thread:

en.cppreference.com/w/cpp/memory/shared_ptr
twitter.com/SFWRedditGifs

Stay there, we're full.

std::unique_ptr

elaborate

C++ dev's uppity stereotype never ceases to amaze

>using namespace std

Unique pointer is supposed to express exclusive ownership, so he's creating an exclusive pointer to a shared pointer, which is just trying to be funny I guess.

What happens to the underlying object in that case? Can it only be passed by value?

>What happens to the underlying object in that case?
Nothing, it's a pointer to a pointer to the object, not a pointer to the object.

unique_ptr wraps a pointer to the object and ensures automatic destruction (i.e. RAII).

It is "unique" because the copy constructor and copy assignment operators are deleted,. Hence you can't pass unique_ptr by value. You can access and pass the raw pointer to the object via ->get(). This can be passed around freely.

If you intend to change ownership, unique_ptr can be moved as it implements move constructor and move assignment constructor. You can think of these as functions that will be selected at overload resolution instead of the copy constructor/assignment operator. Only move a unique_ptr if you intend to change ownership. If you only want to use the underlying object in functions, use raw_ptr.

A shared_ptr can be passed by value. Any time it is copied, its reference count will increment, and likewise decrement upon destruction. When the reference count reaches zero, the underlying object will be destroyed.

You can wrap a shared_ptr in a unique_ptr, but it is pointless.

by raw_ptr I meant to be the raw pointer accessed via ->get()

Great explanation, user.

and by move assignment constructor I meant move assignment operator.

Attached: brailnet.jpg (800x450, 41K)

Complete brainlet here and honest question.

What are some 'useful' or 'practical' differences between handling objects with shared_ptr and handling objects with polymorphism + arrays?

Two completely unrelated things dude

>polymorphism + arrays
What did you even mean by this?

Damn. Then I didn't understand shared_ptr. Back to brainlet land with me.

shared_ptr = reference counted pointer

If I got that right. A shared_ptr has a reference counter. If it runs out of scrope the counter is decremented by 1. If it hits 0, it automatically calls the destructor.

ty

shared_ptr is just a wrapper around pointers ensuring that once all the shared_ptrs pointing to an object are destroyed, the pointed object is destroyed too.
void foo() {
auto object = new MyObject();
auto object2 = object;
// ...
return;
// object and object2 are destroyed, but the original object pointed to hasn’t been destroyed. Memory leak
}

void foo2() {
auto object = std::make_shared();
auto object2 = object;
// ...
return;
// object and object2 are destroyed, when the last one is destroyed it also destroys the original pointed object
}


en.cppreference.com/w/cpp/memory/shared_ptr

Also, if you need any reference for standard C++, use cppreference.com, cplusplus.com is shit and outdated

If by polymorphism + arrays you mean a collection of pointer to base class, the only difference will be that in case of using shared_ptr the lifetime of the objects will be managed by reference count.

You can do std::vector as well as std::vector, but in the former case you are responsible for destroying the objects.

Thanks mates, I understand this thing now.

>If by polymorphism + arrays you mean a collection of pointer to base class
Yeah something like that, like "objects -sharing- some base class" but shared_ptr is something completely different.

Just use C++ when speed is essential, otherwise C# is a much better option Kek

Don't expect your hands to be held in a walled garden
This, 2bh.

Attached: 1521809836508.jpg (800x800, 79K)