C ++ struct and typdef

I am not sure about the name because I do not know what it is.

I tried to understand the code in the link below.

Color Based Particle Filter

I generally understand what the program does, but I could not understand "state.h". What does this code do? Especially the details of "typedef", "State_" and "pp" look unfamiliar to me.

I have placed part of the code here for clarity.

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 help is appreciated.

+7
c ++
source share
4 answers

In short: State is an alias for a function pointer. State_ is a wrapper class that has a State member and is implicitly converted to that State .

The reason State_ is needed is because there is no way to express a function that returns a pointer to a function of the same type. The wrapper gets rid of self-reference.


Line by line:

 struct StateData; 

Direct declaration of the StateData class.

 struct State_; 

The first declaration of the State_ class.

 typedef State_ (*State)(StateData&); 

This is a bit complicated. It defines State as a type alias for a function pointer, which can point to a function that returns State_ , and takes StateData& as an argument. Functions declared at the end of a fragment can be indicated by a pointer to a function of this type.

In my opinion, the chosen name is very confusing, given that the State_ class already exists. Although I usually oppose Hungarian notation, I would recommend always using a suffix or prefix to indicate a function pointer, like state_fun or state_handler or state_callback ,

 struct State_ { 

This starts the definition of State_ calss.

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

This defines the constructor for the class. The argument has a function pointer type that was previously defined. It initializes a member to be announced shortly.

  operator State() { return p; } 

Member function. More specifically, custom conversion to a function pointer type.

  State p; 

Declares an element that has been 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 State can point to.

+5
source share

State_ - type of structure.

State is a function pointer that takes a parameter of type StateData& and returns State_ .

typedef ab; defines a type b that exactly matches a .

p - field of the State_ class, pp - constructor parameter. p(pp) is special constructor-only syntax that initializes p to pp

+3
source share

This declares a structure called StateData p>

 struct StateData; 

The typedef keyword is used to create a new alias for a type
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 of the advantages of this is that you can easily change the data type of a numeric variable, especially if you have bulk instances of 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 declares a structure called State _

 struct State_; 

This defines the State_ that was declared above.

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

This is the list initializing p . it initializes the declared p inside the structure (i, e. State p );

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

These prototype functions return State_, which accept a reference to StateData p>

 State_ state_start(StateData& d); State_ state_selecting(StateData& d); State_ state_initializing(StateData& d); State_ state_tracking(StateData& d); 
+1
source share

typedef helps when defining a function pointer. You have a function with the following heading. State_ is the return type, and input is a reference to StateData.

 State_ function (StateData&) 

Then just add star (and parens to resolve priority) to describe a pointer to this function.

 State_ (*State) (StateData&) 

Then put a typedef in front of this to define it as a new type. You now have a new type that you can use elsewhere. typedef is used to create type aliases, which makes your code more readable.

 typedef State_ (*State) (StateData&) 

Now this type state can be used elsewhere. Declare a new variable p using this new type

 State p 

Use new type of state for constructor input

 State_(State pp) 

The ability to specify types of function pointer using typedef makes write functions that accept other functions because input is very readable.

+1
source share

All Articles