Movies.cpp
Movies::Movies() {movies = new std::vector<Movie>;}
Movies::~Movies(){
delete movies;
}
in this snippet (theres a class movies) movies is a pointer to a vector of class movie objects
do i need to explictly delete it in the destructor or will the destructor take care of that??
I think you need to delete it explicitly in destructor, otherwise you may have a memory leak
hmm, ohkk thanks
Why do you need the movies vector to be a pointer? What design rationale motivates that? The decision to make it a pointer, which is questionable, is complicating your code; that is why you now need the new allocation, and subsequent delete.
Why do you need the movies vector to be a pointer? What design rationale motivates that? The decision to make it a pointer, which is questionable, is complicating your code; that is why you now need the new allocation, and subsequent delete. Make it a stack object, and the default constructor and destructor become trivial; remove them and let the compiler define it's defaults. Remember, the vector object will be created on the stack, while it automatically manages the Movie objects on the free store (heap) for you.
Why do you need the movies vector to be a pointer? What design rationale motivates that? The decision to make it a pointer, which is questionable, is complicating your code; that is why you now need the new allocation, and subsequent delete. Make it a stack object, and the default constructor and destructor become trivial; remove them and let the compiler define it's defaults. That way, you get the move and copy operations for free, defined by the compiler. As it stands, your code violates the Rule of 6 critical guideline (please read up on this very important and critical subject); basically, if you DEFINE ANY ONE of the following, you PROBABLY SHOULD DEFINE THEM (ALL SIX) ALL; - Default constructor. - Destructor. - Move constructor. - Move assignment operator. - Copy constructor. - Copy assignment operator. Remember, the vector object itself will be created on the stack, while it automatically manages the Movie objects on the free store (heap) for you. Therefore, making it a pointer has no clear benefit here, and is the cause of extra, unnecessary, and error prone work.
Why do you need the movies std::vector to be a pointer? What design rationale motivates that? The decision to make it a pointer, which is questionable, is complicating your code; that is why you now need the new allocation, and subsequent delete. Make it a stack object, and the default constructor and destructor become trivial; remove them and let the compiler define it's defaults. That way, you also get the move and copy operations for free, correctly defined by the compiler. As it stands, your code violates the RULE OF 6 (RULE OF SIX) critical guideline (please read up on this very important and critical subject); basically, if you DEFINE ANY ONE of the following, you PROBABLY SHOULD DEFINE THEM ALL (ALL SIX); - Default constructor. - Destructor. - Move constructor. - Move assignment operator. - Copy constructor. - Copy assignment operator. Remember, the std::vector object itself will be created on the stack, while it automatically manages the Movie objects on the free store (heap) for you. Therefore, making it a pointer has no clear benefit here, and is the cause of extra, unnecessary, and error prone work.
Thanks, but it was not a pointer originally.i was just trying something out.
Обсуждают сегодня