Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef of anonymous class

Tags:

c++

c++11

Is there any difference in any aspect (syntactic limitation, performance, etc.) between the following two definitions?

using Foo = struct { int a, b, c; };

struct Foo { int a, b, c; };

(I'm asking because the first form is aesthetically more uniform when put among a lot of using declarations.)

EDIT: The post linked to in the comment doesn't exactly answer my question. I'm more concerned about how the above two definitions differ in terms of usage, whereas that post mainly answers how they are different in terms of, well, what they are, I think.

like image 452
Zizheng Tai Avatar asked Jul 18 '16 21:07

Zizheng Tai


People also ask

Can you typedef a class?

In C++, a typedef name must be different from any class type name declared within the same scope. If the typedef name is the same as a class type name, it can only be so if that typedef is a synonym of the class name. A C++ class defined in a typedef definition without being named is given a dummy name.

What is anonymous class in c++?

A class with no name provided is known as an anonymous class in c++. An anonymous class is a special class with one basic property. As there is no name given to the class there is no constructor allocated to it, though a destructor is there for deallocating the memory block.

Can anonymous class have destructor?

Anonymous classes: Cannot have a constructor or destructor.


1 Answers

Here are some differences I can think of:

  • (obvious) You can't declare any constructors, a destructor, or an assignment operator for an unnamed class.
  • You can't forward-declare an unnamed class, including as a friend of another class.
  • You can't mark an unnamed class final.
  • struct Foo can be declared in the same declarative region as a function or variable named Foo, although, obviously, you shouldn't do this. using Foo = ... does not allow you this freedom.
like image 199
Brian Bi Avatar answered Oct 05 '22 16:10

Brian Bi