C++ Class Constructors and Destructors Access Specifiers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ Class Constructors and Destructors Access Specifiers

Hello, I notice that when I define a default constructor under private: in a class, it does not execute, but when I put a destructor under private, it produces an error when I declare a variable of type class. Why is that? https://code.sololearn.com/cc4txYWRo0yU/#cpp

6th Sep 2020, 5:13 AM
Edward Finkelstein
Edward Finkelstein - avatar
3 Answers
+ 4
Because when you put a constructor inside the private section of the class, default constructor gets executed instead of the one defined by you. Whereas in the case of destructors, you can't have more than one for a class thus as and when you define one as private, object can no longer access it, and hence generate an error.
6th Sep 2020, 5:20 AM
Arsenic
Arsenic - avatar
+ 2
Your welcome Edward Finkelstein 🙂
7th Sep 2020, 12:33 AM
Arsenic
Arsenic - avatar
+ 1
thank you Arsenic
6th Sep 2020, 5:10 PM
Edward Finkelstein
Edward Finkelstein - avatar