什么是这个疯狂的C ++ 11语法==> struct:bar {} foo {} ;? [英] What is this crazy C++11 syntax ==> struct : bar {} foo {};?
问题描述
这在C ++ 11中可能意味着什么?
What could this possibly mean in C++11?
struct : bar {} foo {};
推荐答案
首先, UDT(用户定义类型):
First, we'll take a bog-standard abstract UDT (User-Defined Type):
struct foo { virtual void f() = 0; }; // normal abstract type
foo obj;
// error: cannot declare variable 'obj' to be of abstract type 'foo'
让我们还记得我们可以在定义UDT的同时实例化UDT:
Let's also recall that we can instantiate the UDT at the same time that we define it:
struct foo { foo() { cout << "!"; } }; // just a definition
struct foo { foo() { cout << "!"; } } instance; // so much more
// Output: "!"
让我们结合例子,回想一下,我们可以定义一个UDT, / em>:
Let's combine the examples, and recall that we can define a UDT that has no name:
struct { virtual void f() = 0; } instance; // unnamed abstract type
// error: cannot declare variable 'instance' to be of abstract type '<anonymous struct>'
我们不再需要有关匿名UDT的证明,所以我们可以失去纯虚函数。还将实例重命名为
foo
,我们留下:
We don't need the proof about the anonymous UDT any more, so we can lose the pure virtual function. Also renaming instance
to foo
, we're left with:
struct {} foo;
接近。
现在,如果这个匿名UDT是从某个基础派生的,那么该怎么办?
Now, what if this anonymous UDT were to derive from some base?
struct bar {}; // base UDT
struct : bar {} foo; // anonymous derived UDT, and instance thereof
,C ++ 11引入了扩展初始化,这样我们可以这样混淆:
Finally, C++11 introduces extended initialisers, such that we can do confusing things like this:
int x{0};
并且:
int x{};
最后,
struct : bar {} foo {};
这是一个未命名的结构bar,实例化为foo,并使用空白初始值程序。
这篇关于什么是这个疯狂的C ++ 11语法==> struct:bar {} foo {} ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!