So I have read that using new
means that you manually have to manage the memory while using automatic variables means that it will get deleted when the variable goes out of scope. How does this work with constructors? If I create an object using an automatic variable, will it be saved or not?
For example, if i have a class:
class University{
Student s;
public:
University(int id, char* name){
Student x(id, name);
s = x;
}
};
Would this work? (Assuming I had a properly defined copy constructor for the class Student). If this does work, why would anyone want to use new
over this? I'm very new to C++ so apologies if this is a stupid question.