Click here to Skip to main content
15,886,644 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
in C++ , you can declare a class without name .

like
C++
class 
{

};


Above declarations is perfectlly legal in C++. It will compile without any error.

My question is ,

Though it is allowed, is ther any a specific use of this ?
or it is a loophole ??
Posted

Anonymous classes are allowed (see, for instance, MSDN[^]).
I think they are not a loophole.
 
Share this answer
 
You may declare instances of that class.
C++
class{public: int x;} xaaa1, xbbb1, xccc1;
class{public: int y;} xaaa2, xbbb2, xccc2;

int main()
{
    xaaa1.x = 1;
    xaaa2.y = 2;
    xbbb2.y = 3;
    cout<< "xaaa1.x = "<< xaaa1.x<< ";  xaaa2.y = "<<  xaaa2.y
        << "; xbbb2.y = "<<  xbbb2.y<< "; xaaa1.x +  xaaa2.y + xbbb2.y = "
        << xaaa1.x +  xaaa2.y + xbbb2.y<< endl;


You are not obligated to instanciate the class.
For instance, this is not a problem:
C++
class bazz{} a;
int main()
{
    bazz b;
    bazz;
    int;
 
Share this answer
 
Comments
Crimson Red 20-Jun-21 17:55pm    
class bazz{} a;
int main()
{
bazz b;
bazz;
int;

I couldn't get this code can you please clarify.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900