Structure Declaration

Hello everyone,
This is my first post so please bear with me.
I am struggling to understand the structure declaration in the following piece of code. This code is public domain on github.It is a portion of a header file "autopilot.h"
What is the programmer trying to do here?
Instantiating a variable, in this case "Route", from within the structure Route?
Is this even allowed by the language.
There isn't another variable inside the header file with the same name or another struct with a "Route" member.
Or perhaps simply creating a new variable with the same name?
I am deeply confused with this.
Any insights will much appreciated
Cheers,
Alexis

1
2
3
4
5
6
7
8
9
10
11
12
  struct Route{
  int nTurnouts;
  int *t;
  static EggTimer routeTimer;
  static int cTurnout;
  static Route *cRoute;    //Help needed here
  static int callBack;
  static void (*eFunc)(int);
  Route(int n, int *t);    //help needed here
  static void setTurnout(int s);
  static void activate(Route *r, int s, void (*)(int)=NULL, int=0); //help //needed here as well
};// end Route 
Perhaps this will give you some idea of what to search for.

Line 6: static Route *cRoute;
Declares a pointer to Route named cRoute.

Line 9: Route(int n, int *t);
Declares a special member function called a constructor. The constructor creates an instance of class Route.
See a tutorial on classes, perhaps at this site: http://www.cplusplus.com/doc/tutorial/classes/

Line 11: static void activate(Route *r, int s, void (*)(int)=NULL, int=0);
Declares a member function named activate which returns nothing and accepts between two and four arguments.

The first parameter is a pointer to Route named r. The second one is an int named s. The third parameter is an anonymous pointer to function returning nothing and accepting an int. Its' default value is NULL.
The fourth one is an anonymous int, whose default value is 0.
http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_functions
http://en.cppreference.com/w/cpp/language/default_arguments
Last edited on
mbozzi,
thanks for the reply. I think you gave me enough material to look through.
If I continue have difficulties I will certainly post a question again.
Ill go a tiny bit deeper.


static Route *cRoute; //Help needed here

Here, because it is static, this pointer has the same value for all objects of this class. That is, if you have
Route a;
a.cRoute = &other_route;
Route b; //b.cRoute is ALSO &other_route, the memory location is shared!!

If it did not have that keyword static, each copy of cRoute would be unique to its variable, a and b could have different values.

Without the static, note (this is not related to the code you are looking at!!)

Where the object has a pointer to its own type you often have a linked list/tree/graph/similar data structure where you have a main entry point variable of the type and those can have 0 to infinite children tacked onto them. Such pointers can be used for other purposes as well, just note that its a pointer to the same type, allowing infinite chains. This isn't the case here, but you need to understand that it is often important when a type has a pointer to itself inside.

Topic archived. No new replies allowed.