Can we create a Dynamic Object in C++ if there weren't any default or overload constructors in the class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we create a Dynamic Object in C++ if there weren't any default or overload constructors in the class

I want to know whether there's a way of creating dynamic objects if there weren't any Constructors implemented in the class

23rd Sep 2020, 8:22 PM
Sachintha Wickramasinghe
Sachintha Wickramasinghe - avatar
2 Answers
+ 1
Yes you can. If you "forget" to define any constructor,then I pretty sure you know that the compiler will create a default one for you.Thus you'll still be able to create objects of that class. If by the phrase "If there weren't any default or *parameterized constructors" you mean that the programmer explicitly deleted all possible constructors,then technically you can still be able to create objects of that class. All you have to do is allocate memory ,either on the stack or on the heap,then reinterpret_cast the buffer's pointer to a pointer of that class. Though honestly I don't know why anyone would want to do this.:-)
24th Sep 2020, 8:46 AM
Anthony Maina
Anthony Maina - avatar
+ 1
Thank you so much. I just wanted to know whether it's possible 🌝
24th Sep 2020, 10:46 AM
Sachintha Wickramasinghe
Sachintha Wickramasinghe - avatar