开发者

C++ inline class definition and object initialisation

I've just come across the following code:

#include <iostream>

static class Foo
{
public:
    Foo()
    {
        std::cout << "HELLO" << std::endl;
    }

    void foo()
    {
        std::cout << "in foo" << std::endl;
    }

}
    blah开发者_如何转开发;

int main()
{
    std::cout << "exiting" << std::endl;
    blah.foo();
    return 0;
}

I haven't seen the above method of definining a variable before - the class definition is done inline with the variable definition. It reminds me of anonymous classes in Java. What is this called, and is it in the C++ standard?

Thanks

Taras


It's quite standard to define a class (or struct, perfectly equivalent except that the default is public instead of private) and declare a variable of its type (or pointer to such a variable, etc) -- it was OK in C (with struct, but as I already mentioned C++'s class, save for public vs private, is the same thing as struct) and C++ mostly maintains upwards compatibility with (ISO-1989) C. Never heard it called by any special name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜