什么可以保留在“struct”和“{”除了结构名称? [英] What can stay between "struct" and "{" except the structure name?

查看:127
本文介绍了什么可以保留在“struct”和“{”除了结构名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里给出了如何在C ++中使用数据结构的一个清楚的例子。 1 这是链接页面上给出的示例之一:

  struct product {
int weight;
float price;
};

产品苹果;
product banana,melon;

但是,我有一个不遵循此模板的代码,我不明白。我有以下给定:

  struct result:mppp :: data :: table< 
row< semantics :: user,int>,
row< semantics :: exitdatum,spmm :: date>,
row< userid,int&
> {};

我不明白为什么而不是结构名称我们有这么复杂的结构, 。此外,我不明白为什么结构体的body是空的({和}之间没有任何内容)。



任何人都可以解释一下



感谢您的答案。

现在更清楚了。上面示例中的表示继承。但是这些结构的意思: aaa

解决方案

该代码使用继承。您可以在名称后面指定 struct 的基类,使用字符分隔它们, public protected private 限定符之一指定继承类型( public 是默认值,如果没有指定(*)):

  struct A {}; // Fine 

struct B:public A {}; //也很好

struct C:B {}; //再次很好,`public`默认是假设

struct D:A,B {}; //也可能(多重继承)

struct E {};

struct F:public E,private D {} //限定符可以不同

struct:A,F {} obj; // structs可以是匿名的



在你的情况下,基类是模板的一个实例: / p>

  template< typename T> 
struct X {};

struct Y:X< A> {}; // Fine



(*)虽然相同的限定符适用于 class 类型的继承,但在这种情况下默认为 private


A clear example of how data structures can be used in C++ is given [here].1 This is one of the examples given on the linked page:

struct product {
  int weight;
  float price;
} ;

product apple;
product banana, melon;

However, I have a code that does not follow this template and I cannot understand it. What I have is given bellow:

struct result : mppp::data::table <
    row<semantics::user,       int>,
    row<semantics::exitdatum, spmm::date>,
    row<userid,                int>
> {};

I do not understand why instead of struct name we have such a complex construction and how it should be understood. Moreover, I do not understand why the "body" of the struct is empty (there is nothing between "{" and "}").

Can anybody please explain me that?

ADDED

Thank you for the answers. Now it is more clear. The : in the above example means inheritance. But what all these structures mean: aaa<bbb>?

解决方案

That code uses inheritance. You can specify the base classes of a struct after their name, separating them with a : character and, possibly, using one of the public, protected, or private qualifiers for specifying the type of inheritance (public being the default if none is specified (*)):

struct A { }; // Fine

struct B : public A { }; // Also fine

struct C : B { }; // Fine again, `public` is assumed by default

struct D : A, B { }; // Also possible (multiple inheritance)

struct E { };

struct F : public E, private D { } // Qualifiers can differ

struct : A, F { } obj; // structs can be anonymous 

In your case, the base class is an instance of a template:

template<typename T>
struct X { };

struct Y : X<A> { }; // Fine


(*) It is also worth mentioning that while the same qualifiers apply to inheritance for class types, the default is assumed to be private in that case.

这篇关于什么可以保留在“struct”和“{”除了结构名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆