D
D
Daniil Demidko2015-10-30 07:52:54
C++ / C#
Daniil Demidko, 2015-10-30 07:52:54

The "class{}Name;" construct instead of "class Name{};"?

I have always created classes and structures like this:

class ClassName
{
   //...
};

I recently noticed an interesting (for me at least) construction in someone else's code:
class
{
  //...
}ClassName;

Which works like a static class in C#, or like a class in C++ where all members are declared static. Instances naturally are not created.
I was seized with a terrible interest.
Google turned up nothing.
Until now, I was not aware of the existence of static classes in C++.
Stroustrup has nothing either.
Can anyone explain this feature to me in detail?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Tsilyurik, 2015-10-30
@Daniro_San

class ClassName {
   //...
};

- creates a description of the class as a type that you can use in the future; no objects are created in the code.
class {
  //...
} ClassName;

- creates a description of the structure of an anonymous (not named) class and immediately creates an object with the name ClassName (therefore, the name is very unfortunate, ObjName would be appropriate); since the class has no name, you won't be able to create objects of that class anymore... except like this:
class {
  //...
} Obj1, Obj2, Obj3, ...;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question