14

There's a feature called anonymous class in C++. It's similar with anonymous struct in C. I think this feature is invented because of some needs, but I can't figure out what that is.

Can I have some example which really needs anonymous class?

eonil
  • 83,476
  • 81
  • 317
  • 516
  • 1
    Why do you have anonymous struct? – Nawaz Apr 09 '12 at 11:05
  • @Nawaz Ah yeah, you're right. I could be same reason with why it exist on C. Anyway I'm curious about cases specifically for C++. Because C++ is a lot different language with C. – eonil Apr 09 '12 at 12:27

6 Answers6

19

The feature is there because struct and class are the same thing - anything you can do with one, you can do with the other. It serves exactly the same purpose as an anonymous struct in C; when you want to group some stuff together and declare one or more instances of it, but don't need to refer to that type by name.

It's less commonly used in C++, partly because C++ designs tend to be more type-oriented, and partly because you can't declare constructors or destructors for anonymous classes.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
4

It is not really needed in a strict sense and never was. I.e. you could always assign a name, for example anonymous1, anonymous2 etc. But keeping track of more names than necessary is always a hassle.

Where it is helpfull is at any place where one wants to group data without giving a name to that group. I could come up with a several examples:

class foo {
  class {
  public:
    void validate( int x ) { m_x = x; }
    bool valid() { return m_exists; }
  private:
    int m_x;
    bool m_exists;
  } maybe_x;
};

In this case the int and the bool logically belong together, so it makes sense to group them. However for this concrete example it probably makes sense to create an actual optional type or use one of the available ones, because this pattern is most likely used at other places as well. In other cases this pattern of grouping might be so special, that it deserves to stay in that class only.

I really do assume though, that anonymous classes are rarely used (I have only used them a couple of times in my live probably). Often when one want's to group data, this is not class or scope specific but also a grouping which also makes sense at other places.

LiKao
  • 10,408
  • 6
  • 53
  • 91
  • 1
    Your example would need a constructor to ensure the class invariant (that either the value is valid, or the flag is false); but you can't declare a constructor for an anonymous class. – Mike Seymour Apr 09 '12 at 11:29
  • @MikeSeymour: good point. I guess one would have to take care of this some other way. Maybe with C++11 in class initializer this would do. As I said this is most likely a very rarely used feature. – LiKao Apr 09 '12 at 21:27
2

Maybe it was sometimes helpful to make nested functions like:

void foo() {
  class {
    void operator()(){
    }
  } bar;
  bar();
}

But now we have lambdas and anonymous classes are left only for compatibility reasons.

Hauleth
  • 22,873
  • 4
  • 61
  • 112
2

The use of anonymous classes is for preserving compatibility with existing C code. Example:

In some C code, the use of typedef in conjunction with anonymous structures is prevalent.

IndieProgrammer
  • 579
  • 1
  • 5
  • 14
1

There is an example of anonymous structs that can be used with Qt 5's Signal/Slot system with ANY class and without the QObject derivative requirement:

void WorkspaceWidget::wwShowEvent()
{
    //Show event: query a reload of the saved state and geometry
    gcmessage("wwShowEvent "+ this->title());
    struct{void* t; void operator()(){ static_cast<WorkspaceWidget*>(t)->wwReloadWindowState(); }}f;
    f.t=this;
    QObject::connect( &reloadStateTimer, &QTimer::timeout, f);
    reloadStateTimer.start();
}

void WorkspaceWidget::wwReloadWindowState()
{
    gcmessage( dynamic_cast<QObject*>(this)->metaObject()->className());
}

Basically, I need to connect a timer signal to a non-QObject derived class, but want to pass mt "this" properly.

QObject::connect can be connected to ordinary function in Qt 5, so this anonymous class is actually a functor that keeps the this pointer in itself, still passing the slot connection.

Петър Петров
  • 1,966
  • 1
  • 17
  • 14
0

Also you can do things with auto in anonymous (vs2015)

struct {

    auto* operator->() {return this;}
    //do other functions

} mystruct;