hello people,
Currently i'm learning C++. My book explains Dynamic Memory a bit like this:
-You have to now the "value" of a static array when you write the program. But with a dynamic array you can do this in run time.
Dynamic Memory is basically a pointer in a class. This pointer will be pointing to a block of memory (array). You have to write a destructor for this, like this:
class IntArray{
private:
int amountOfValuesInArray;
int *p;
public:
IntArray(amount)
:amountOfValuesInArray(amount){
p = new int [amountOfValuesInArray]
}
ETC.
But then I get to smart pointers: Smart pointers don't need a destructor, but will "free" the memory by themselves, and then I get the following example (I copied it and it isn't in english, I hope it's understandable, otherwise I will translate it(tell me it)):
class Student {
private:
string naam, opleiding, geslacht;
int nummer;
public:
Student( string n, string opl, string gesl, int nr )
: naam(n), opleiding(opl), geslacht(gesl), nummer(nr) {
}
string toString() const {
ostringstream os;
os << naam << ", " << opleiding << ", " << geslacht;
os << ", " << nummer << endl;
return os.str();
}
};
int main() {
//shared_ptr<Student> sp( new Student( "Gertjan", "wiskunde", "m", 313 ) );
auto sp = make_shared<Student>( "Gertjan", "wiskunde", "m", 313 );
cout << sp -> toString() << endl;
cin.get();
}
And the book says that at the end nothing is pointing towards the memory of sp anymore, so the memory will be "released".
But in this class there isn't any dynamic memory (array/pointer) right, so I think I don't understand it. Could you tell me what I understand wrong and how it works then?
Thanks