简体   繁体   English

C ++ struct和typdef

[英]C++ struct and typdef

I am not sure about the title because I don't know what it is really about. 我不确定标题,因为我不知道它的真正含义。

I was trying to understand the code in the link below. 我试图理解下面链接中的代码。

Color Based Particle Filter 彩色粒子滤波器

I generally understand what the program does, but I couldn't figure out "state.h". 我一般都了解程序的功能,但我无法弄清楚“state.h”。 What does this code do? 这段代码有什么作用? Especially the "typedef", "State_" and "pp" parts look stranger to me. 特别是“typedef”,“State_”和“pp”部分对我来说看起来很奇怪。

I will put some of the code here for clarity reasons. 为清晰起见,我会在这里放一些代码。

struct StateData;
struct State_;
typedef State_ (*State)(StateData&);
struct State_
{
    State_( State pp ) : p( pp ) { }
    operator State()
    {
        return p;
    }
    State p;
};

State_ state_start(StateData& d);
State_ state_selecting(StateData& d);
State_ state_initializing(StateData& d);
State_ state_tracking(StateData& d);

Any kind of help would be greatly appreciated. 任何形式的帮助将不胜感激。

Concisely: State is an alias for function pointer. 简明扼要: State是函数指针的别名。 State_ is a wrapper class that has a State member, and is implicitly convertible into that State . State_是一个具有State成员的包装类,可以隐式转换为该State

The reason why State_ wrapper is needed, is because there is no way to express a function that returns a pointer to a function of the same type. 需要State_包装器的原因是因为无法表达返回指向同一类型函数的指针的函数。 The wrapper gets rid of the self-reference. 包装器摆脱了自我引用。


Line by line: 逐行:

struct StateData;

A forward declaration of a class StateData . StateData前向声明。

struct State_;

A forward declaration of a class State_ . State_前向声明。

typedef State_ (*State)(StateData&);

This one is a bit tricky. 这个有点棘手。 It defines State as a type alias for a function pointer that can point to a function that returns a State_ and takes a StateData& as an argument. 它将State定义为函数指针的类型别名,该函数指针可以指向返回State_并将StateData&作为参数的函数。 The functions declared at the end of the snippet can be pointed by a function pointer of this type. 在代码片段末尾声明的函数可以由此类型的函数指针指向。

In my opinion, the chosen name is very confusing considering that there is already a State_ class. 在我看来,考虑到已经存在State_类,所选择的名称非常混乱。 While I'm usually against hungarian notation, I would recommend to always apply a suffix or prefix to denote a function pointer, say state_fun or state_handler or state_callback , 虽然我通常反对匈牙利表示法,但我建议总是应用后缀或前缀来表示函数指针,比如state_funstate_handlerstate_callback

struct State_
{

This starts the definition of State_ calss. 这开始了State_的定义。

    State_( State pp ) : p( pp ) { }

This defines a constructor for the class. 这定义了该类的构造函数。 The argument is of the function pointer type that was defined earlier. 参数是先前定义的函数指针类型。 It initializes the member that will be declared shortly. 它初始化将很快声明的成员。

    operator State()
    {
        return p;
    }

A member function. 会员功能。 More specifically, a user defined conversion into the function pointer type. 更具体地说,用户定义了转换为函数指针类型。

    State p;

Declares the member that was initialized in the constructor. 声明在构造函数中初始化的成员。

};

State_ state_start(StateData& d);
State_ state_selecting(StateData& d);
State_ state_initializing(StateData& d);
State_ state_tracking(StateData& d);

Free functions, that can be pointed by a State . 免费功能,可由State指出。

State_ is a structure type. State_是结构类型。

State is a pointer to function, accepting a parameter of type StateData& and returning State_ . State是一个指向函数的指针,接受StateData&类型的参数并返回State_

typedef ab; defines a type b which is exactly the same as a . 定义类型ba完全相同。

p is a field of class State_ , pp is a parameter of the constructor. p是类State_的字段, pp是构造函数的参数。 p(pp) is a special syntax for constructor only, initializing p to the value of pp p(pp)是仅用于构造函数的特殊语法,将p初始化为pp的值

This one declares a struct with name StateData 这个声明了一个名为StateData的结构

struct StateData;

The typedef keyword is use create a new alias for a type typedef关键字用于为类型创建新别名
Example: 例:

typedef int mynumber; /// mynumber is an alias of type of int 
mynumber number = 3; /// so you can do this (number is a type of int just the int name is different)

one advantage of this is you can easily change the data type of the number variable specially if you have massive instances of mynumber: 这样做的一个优点是,如果你有大量的mynumber实例,你可以轻松地更改数字变量的数据类型:
Example: 例:

typedef int mynumber;
mynumber number1 = 3;  
mynumber number2 = 4;
mynumber number3 = 5; 
// what if you want to make all this type of double? or a float? easy
// just change the int to a double or float like this
    typedef float mynumber;
    typedef double mynumber;

This one declares a struct with name State_ 这个声明一个名为State_的结构

struct State_;

This one defines State_ that was declared above. 这个定义了上面声明的State_。

struct State_
{
    State_( State pp ) : p( pp ) { }
    operator State()
    {
        return p;
    }
    State p;
};

This one is list initializing the p . 这个是初始化p列表。 it initializes the p that was declared inside the struct (i,e. State p ); 它初始化在struct中声明的p (即e, State p );

State_( State pp ) : p( pp )
{ 
}

These prototype functions returns State_ that accepts reference to StateData 这些原型函数返回State_,它接受对StateData的引用

State_ state_start(StateData& d);
State_ state_selecting(StateData& d);
State_ state_initializing(StateData& d);
State_ state_tracking(StateData& d);

typedef helps when defining pointer to function. typedef在定义函数指针时有帮助。 You have a function with following header. 你有一个具有以下标题的功能。 State_ is the return type and the input is a reference to StateData. State_是返回类型,输入是StateData的引用。

State_ function (StateData&)

Then just add star (and parens to resolve precedence) to describe a pointer to this function. 然后只需添加星(和解析优先级的parens)来描述指向此函数的指针。

State_ (*State) (StateData&)

Then put typedef in front of that to define it as a new type. 然后将typedef放在其前面,将其定义为新类型。 Now you have a new type which can be used at other places. 现在你有了一个可以在其他地方使用的新类型。 typedef is used to create type aliases which makes your code more readable. typedef用于创建类型别名,使您的代码更具可读性。

typedef State_ (*State) (StateData&)

Now this type State can be used in another places. 现在这种类型的状态可以在其他地方使用。 Declare a new variable p using this new type 使用此新类型声明一个新变量p

State p

Use new State type as input to the constructor 使用新的State类型作为构造函数的输入

State_(State pp)

Being able to specify pointer-to-function types using typedef make writing functions that take other functions as input much readable. 能够使用typedef指定指向函数的指针类型使得编写函数将其他函数作为输入可读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM