+ 4
What will happen if a class has no name in c++?
36 ответов
+ 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.
+ 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....
+ 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...
+ 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.
+ 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.
+ 4
Singleton is possible even with non-anonymous classes.
+ 3
no object is outside as CRO otherwise Access Violation Error when trying to use destructor Jayakrishna🇮🇳
+ 2
Jayakrishna🇮🇳 though I'm not an overall fan of GeeksforGeeks explanation here is your answer
https://www.geeksforgeeks.org/destructors-c/
+ 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...
+ 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...
+ 2
Bartas Dausynas No not singleton. You can create more objects
Like
class {
...
} obj1, obj2, obj3,... ;
+ 2
Jayakrishna🇮🇳 again to answer the obj:
outside of class use delete obj...
inside class use destructor...
+ 1
Is that first one also usable..? Or proper way? coffeeunderrun
I thought no.. Correct me if I wrong?
+ 1
Jayakrishna🇮🇳
If a class has no name
In case of constructor
Then constructor cannot be created
Or destructor cannot be created
+ 1
Or else we can create destructor but not construtor
+ 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?
+ 1
Conclusion is??
0
Syntax error.
Edit :
how you mean? Mention please..It's Me
0
BroFar
My question is
class {
..
} obj1;
delete obj;
Is this comes under destructor? Since we are not using destructor function here..
0
M.Raffi.I nope