为什么不编译?匿名类+匿名对象c ++ 17

时间:2017-06-09 13:24:51

标签: c++ anonymous-class anonymous-objects

问题:为什么visual c ++ 17不允许使用匿名类的匿名对象?

允许使用匿名对象(我认为在这种情况下它只是暂时的?):

class Foo
{
public:
    virtual int bar() = 0;
};

int main()
{
    class ConcreteFoo : public Foo
    {
    public:
        int bar() override
        {
            //some operation
        }
    };
    ConcreteFoo{}.bar();
    return 0;
}

允许使用匿名类:

int main()
{
    class : public Foo
    {
    public:
        int bar() override
        {
            //some operation
        }
    } myFoo;
    myFoo.bar();
    return 0;
}

不允许在一起:

int main()
{
    class : public Foo
    {
    public:
        int bar() override
        {
            //some operation
        }
    }{}.bar();
    return 0;
}

为什么不呢?我正在使用 visual c ++ 17

错误是:

1>c:\users\peter\downloads\nationsgamemockup\nationsgamemockup\nationsgamemockup.cpp(37): warning C4094: untagged 'class' declared no symbols
1>c:\users\peter\downloads\nationsgamemockup\nationsgamemockup\nationsgamemockup.cpp(37): error C2143: syntax error: missing ';' before '{'
1>c:\users\peter\downloads\nationsgamemockup\nationsgamemockup\nationsgamemockup.cpp(37): error C2059: syntax error: '.'

0 个答案:

没有答案