What will happen if a class has no name in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What will happen if a class has no name in c++?

29th Aug 2020, 5:48 PM
Sufia Hashmi
Sufia Hashmi - avatar
23 Answers
+ 8
It's Me it is difficult to give a conclusion to a somewhat open-ended question asking "what will happen....." As the examples from ~ swim ~ demonstrate, there are a couple of ways of defining anonymous classes in C++, one being a nested (inner) form. So, to answer what will happen, if there are no syntax errors, nothing bad will happen and your program will run as intended. However, you need to be aware of the potential memory leaks that can occur as warned in the above examples if you forget to explicitly deallocate any dynamically allocated memory within an object of the anonymous class, as C++ has no automatic garbage collection.
31st Aug 2020, 10:33 PM
Sonic
Sonic - avatar
+ 5
coffeeunderrun yes. Thanks for the reminding.... But question is not clear and I expecting a reply from questioner. I Thought he/she means class { } Not class{ } obj1; Ananymos class....
29th Aug 2020, 6:26 PM
Jayakrishna 🇮🇳
+ 5
Constructor can not created.. For constructor you need class name.. Here, you don't have name.. But you can have destructor because you created objects already.. coffeeunderrun correct me if am wrong.... Or for better understanding, pls add an example if anything missing here...
29th Aug 2020, 6:46 PM
Jayakrishna 🇮🇳
+ 5
If the class is unnamed then it can't have a custom constructor/destructor. That means you can't pass an argument to it.
31st Aug 2020, 10:37 AM
Sivanji Singh
Sivanji Singh - avatar
+ 5
Sometimes it is possible to give a different type of name to an anonymous class using typedef in a similar way to how typedef can be used with structs for example.
31st Aug 2020, 10:18 PM
Sonic
Sonic - avatar
+ 4
Singleton is possible even with non-anonymous classes.
30th Aug 2020, 9:45 PM
Sonic
Sonic - avatar
+ 3
no object is outside as CRO otherwise Access Violation Error when trying to use destructor Jayakrishna🇮🇳
29th Aug 2020, 10:11 PM
BroFar
BroFar - avatar
+ 2
Jayakrishna🇮🇳 though I'm not an overall fan of GeeksforGeeks explanation here is your answer https://www.geeksforgeeks.org/destructors-c/
29th Aug 2020, 8:18 PM
BroFar
BroFar - avatar
+ 2
BroFar my doubt about is there any explicit destructor in anonimuos class.. Not normal desctrutor.. If your answering about original question, then am not the questioner.. Questioner asking is there any constructor or destructor for ananimous class..? My answer is It don't have constructor but have implicit desctrutor. Now I got doubt, that I asked above to mam.. Thinking no one like that.. But for a clarity asking...
29th Aug 2020, 8:49 PM
Jayakrishna 🇮🇳
+ 2
The answer above was to your question that you have to create a "destructor per object" to delete the object/s in an anonymous class. Jayakrishna🇮🇳 no there is no explicit destructor in anonymous class...
29th Aug 2020, 8:57 PM
BroFar
BroFar - avatar
+ 2
Bartas Dausynas No not singleton. You can create more objects Like class { ... } obj1, obj2, obj3,... ;
30th Aug 2020, 9:48 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 again to answer the obj: outside of class use delete obj... inside class use destructor...
30th Aug 2020, 11:51 PM
BroFar
BroFar - avatar
+ 1
Is that first one also usable..? Or proper way? coffeeunderrun I thought no.. Correct me if I wrong?
29th Aug 2020, 6:30 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 If a class has no name In case of constructor Then constructor cannot be created Or destructor cannot be created
29th Aug 2020, 6:41 PM
Sufia Hashmi
Sufia Hashmi - avatar
+ 1
Or else we can create destructor but not construtor
29th Aug 2020, 6:43 PM
Sufia Hashmi
Sufia Hashmi - avatar
+ 1
coffeeunderrun thank for answer..... Not sure am about here is : Can we have explicit destructor for that class objects? or we need to use delete for objects.. then is it called as a desctrutor?
29th Aug 2020, 7:09 PM
Jayakrishna 🇮🇳
+ 1
Conclusion is??
31st Aug 2020, 12:19 PM
Sufia Hashmi
Sufia Hashmi - avatar
0
Syntax error. Edit : how you mean? Mention please..It's Me
29th Aug 2020, 6:15 PM
Jayakrishna 🇮🇳
0
BroFar My question is class { .. } obj1; delete obj; Is this comes under destructor? Since we are not using destructor function here..
29th Aug 2020, 9:07 PM
Jayakrishna 🇮🇳
0
31st Aug 2020, 12:17 PM
Sufia Hashmi
Sufia Hashmi - avatar