What can stay between “struct” and “{” except the structure name?

痞子三分冷 提交于 2019-12-23 23:39:01

问题


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>?


回答1:


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.


回答2:


It is inheriting a template, but not adding any fields of its own.

You must read a C++ tutorial.




回答3:


It is inheritance like the case in class. Therefore, in your example, struct result inherits another class or struct mppp::data::table < row<semantics::user, int>, row<semantics::exitdatum, spmm::date>, row<userid, int> >.




回答4:


A struct is equivalent to a class (other than the default access level). You can inherit structs or classes just as well. mppp::data::table < row<semantics::user, int>, row<semantics::exitdatum, spmm::date>, row<userid, int> is just that - a specialized template class.



来源:https://stackoverflow.com/questions/15092844/what-can-stay-between-struct-and-except-the-structure-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!