Normally memory managment is used in the context of dynamic memory that is created by
new
mallo
In the normal code C++ behaves like every other language. If you create a variable or return it, it is copied and accessible on the target side.
int a = addTwo(3);
gets a copy of your returned value. If the returned value is a class copy operator called.
So as long as you do not work with new and malloc you do not have to care about memory managment that much.
One additional remark which is important
void func(std::string abc)
{
// method gets a copy of abc
}
void func(std::string& abc)
{
// method gets the original string object which can be modified without having to return it
}
void func(const std::string& abc)
{
// method gets the original string object abc but is not able to modify it
}
The difference of the three lines is very important because your program may spare a lot of time creating copies of input parameters that you normally didn't want to create.
e.g.
bool CmpString(std::string a, std::string b)
{
return a.compare(b);
}
is really expensive because the atrings a and b are always copied.
Use
bool CmpString(const std::string& a, const std::string& b)
instead.
This is important because no refcounted objects are used by default.