I\'m a seasoned C developer who is just now getting into C++, and I must admit, I\'m very confused about how many ways there are to create, retain, and destroy C++ objects. In C
First of all, your memory management skills are useful in C++, just they are a level below the C++ way of doing things, but they are there...
About your questions, they are a bit broad, so I'll try to keep it short:
1) What are all the ways to create a C++ object?
Same as C: they can be global variables, local automatic, local static or dynamic. You may be confused by the constructor, but simply think that every time you create an object, a constructor is called. Always. Which constructor is simply a matter of what parameters are used when creating the object.
Assignment does not create a new object, it simply copies from one oject to another, (think of memcpy
but smarter).
2) What are all the different initialization syntaxes associated with all these types of object creation? What's the difference between T f = x, T f(x);, T f{x};, etc.?
T f(x)
is the classic way, it simply creates an object of type T
using the constructor that takes x
as argument.T f{x}
is the new C++11 unified syntax, as it can be used to initialize aggregate types (arrays and such), but other than that it is equivalent to the former.T f = x
it depends on whether x
is of type T
. If it is, then it equivalent to the former, but if it is of different type, then it is equivalent to T f = T(x)
. Not that it really matters, because the compiler is allowed to optimize away the extra copy (copy elision).T(x)
. You forgot this one. A temporary object of type T
is created (using the same constructor as above), it is used whereever it happens in the code, and at the end of the current full expression, it is destroyed.T f
. This creates a value of type T
using the default constructor, if available. That is simply a constructor that takes no parameters.T f{}
. Default contructed, but with the new unified syntax. Note that T f()
is not an object of type T
, but instead a function returning T
!.T()
. A temporary object using the default constructor.3) Most importantly, when is it correct to copy/assign/whatever = is in C++, and when do you want to use pointers?
You can use the same as in C. Think of the copy/assignment as if it where a memcpy
. You can also pass references around, but you also may wait a while until you feel comfortable with those. What you should do, is: do not use pointers as auxiliary local variables, use references instead.
4) Finally, what are all these things like shared_ptr, weak_ptr, etc.?
They are tools in your C++ tool belt. You will have to learn through experience and some mistakes...
shared_ptr
use when the ownership of the object is shared.unique_ptr
use when the ownership of the object is unique and unambiguous.weak_ptr
used to break loops in trees of shared_ptr
. They are not detected automatically.vector
. Don't forget this one! Use it to create dynamic arrays of anything.PS: You forgot to ask about destructors. IMO, destructors are what gives C++ its personality, so be sure to use a lot of them!