2008-05-26

anonymous classes

Recently I tend to group semantically related members into nested classes or structs. For instance imagine a class with concurrent access:

class Foo {
public:
    int getSomething() {
        Lock l(purpose.mutex);
        return purpose.something;
    }
private:
    struct {
        Mutex mutex;
        int something;
    } purpose;
    
};

To avoid redundancy I don't name such nested structs. Why should I? C++ provides the feature of anonymous classes and structs and this technique is a good example why one would like to have it.

But as often with C++ this language feature collides with others and makes it less usable. In this case you are getting problems when the grouped members need to be initialized. As a constructor must have the name of the class anonymous classes can not have constructors.

Orthogonality must definitly be a design goal for usable general purpose languages. And this property is greatly missing in C++ which is the matter for my blog entries.


Posted by Alexander Bernauer | Permanent Link | Categories: C++