+ 7

Explain static & dynamic objects in object oriented programming in C++???

🏵🌼💐🌸💮🏵💐🌼🏵🌸

15th Apr 2019, 7:58 AM
Nithya Yamasinghe
Nithya Yamasinghe - avatar
2 Answers
+ 7
Objects are like any variable in essence blocks of memory, this memory is allocated either on the stack or on the heap. Some like to call objects/vars that are allocated on the heap "dynamic". But an object that is allocated on the stack basically works the same as one on the heap. The only difference is that an object that is allocated on the stack has a known scope. After an object allocated on the stack is no longer in scope it gets deleted and the memory that it had occupied gets freed. An object allocated on the heap has an unknown scope, and in C++ the programmer must delete it manually, otherwise, you will create memory leaks. But as I first said the "dynamic" labeling has nothing to do with OOP it's simply describing one way of using memory allocation.
15th Apr 2019, 6:58 PM
Andrei O
Andrei O - avatar