Похожие чаты

File- Movies.h class Movies { private: std::vector <Movie> *movies; File-

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??

7 ответов

16 просмотров

I think you need to delete it explicitly in destructor, otherwise you may have a memory leak

Garry- Автор вопроса

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.

Garry- Автор вопроса
Kenshin Himura
Why do you need the movies std::vector to be a poi...

Thanks, but it was not a pointer originally.i was just trying something out.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта