问题:为什么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: '.'