Team 67 C++ Example Code

We’ve posted Beta Test C++ example teleop code from Team 67 on the FIRST Forums. We’re working on our autonomous code – the skeleton of that can be seen as well.

Dave D

Dave,

I noticed many teams using this format:

->

For instance, something like

Joystick->Set(0)

Can you also use a period:

Joystick.Set(0)

Several websites suggest the two are interchangeable when using classes and objects.

-> and . can not be interchanged.

x->y() is a shortcut for (*x).y(), so you use . when x is an object, and -> when x is an pointer to an object. It is exactly the same as accessing members of a structure in C.

Thanks. For someone who’s never used pointers, classes, objects, members, and structures trying to read this code is brutal.

I still don’t get the reason behind wanting to use a pointer rather than just a variable.

Say you have a structure. It has a variable in it that you want to modify with a function. If you were to just pass the structure into the function, the compiler would pass a copy of the structure to the function, so any changes you did to the structure wouldn’t change the original. This isn’t the behavior you want. So instead, you pass a pointer to the structure to the function. That pointer is a number saying where the structure is in memory, so when the compiler makes a copy of the pointer to pass into the function, that still points to the correct place in memory. You can then modify the structure in your function, and it all works.

I can try to clarify later, if someone else doesn’t beat me to it, if that was confusing.

You explanation is clear (I’ve spent the last couple days reading about classes, objects, structures, constructors, and pointers).

When I learned programming - and perhaps I’m dating myself here - I was always taught you write a function to return a single value. Send the values you need into the procedure, and it returns a result.

In a case where you MUST have a procedure return multiple results (and I was always taught that this was sloppy programming) you could always pass by reference, or use an (evil evil) global variable.

See Kevin Watson’s C-code for an example of how I was taught to code using functions.

I’m sure I’m missing something since the whole class / pointer system is new to me. But I’m struggling to see a reason to program using them as opposed to using functions and returning values. This is probably just because I haven’t gotten into the level of complexity where they become useful.

With C++, it starts becoming useful when you get into inheritance, and all the other OOP stuff. I haven’t checked the new WPI c++ libs to see if this is a valid example, but a Victor class and Jaguar class could both inherit a motor controller class, and everywhere in your code where you manipulate an instance of a motor controller, it could really be an instance of a Victor class or a Jaguar class, and you don’t have to know. So you could swap out your Victor for a Jaguar, change 1 line in your code where you create an instance of the class, and all your other code using the motor controller would still work (providing victors and jaguars don’t have that different electrical characteristics).

Pointers are also available in last year’s code also, but they weren’t used much since you couldn’t really allocate memory, and didn’t have a need for it. I believe my team used pointers to pass in a structure containing our PID constants and the variable storing up the data to use for the I part of PID and the D part of PID. The function would return the motor power and update the structure.

If there was a C library available for the bot, we could easily program everything without pointers or structures, so most of this isn’t exactly necessary, but instead allows us to do more. My personal opinion is that you can do everything you need to in C with pointers and structures, and not need classes, but it takes a bit more thinking and planning.

A main reason that pointers are used was mentioned above: to reduce the number of copies that need to be done to perform your functionality.

Take image processing for example. Say you have a function that takes an image data structure as an input and the goal is to invert the color. In a pass by value case, your function and call may look like

ImageType invert(ImageType input)
{
   ImageType output;
   /* do stuff to populate output with inverted input*/
   return output;
}



ImageType img;
/* assume the image is populated at this point */
img = invert(img);

In this case, there are 3 copies of the image floating around. If the images are 5M a piece, that’s 15M. Those large copies can really slow your system down; especially in a system with a small amount of resources.

If you take the pass by reference case, you would have something like

void invert(ImageType *img)
{
   /* do stuff to invert img*/
}





ImageType img;
/* assume the image is populated at this point */
invert(&img);

Here, there is only one copy of the image. The passed argument is a pointer, and is most definitely smaller than the 5M image.

Pointers take some getting used to, but they can greatly improve your performance when passing large amounts of data around.

The only caveat is that it only improves things if your processor supports pointers well. In the recent IFI controllers, there was no native pointer support and pointer processing required many, many lines of assembly to accomplish. In that case, it was more efficient to pass the data around by value.

Another big reason for using pointers is to support multiple return values. Say you wanted had a function that took in an integer i , and returned both 2 * i and 3 * i. You can’t really do this directly with pass by value (you could if you returned it in a struct), but with pass by reference, you can do the following

void func(int i, int *j, int *k)
{
  *j = 2 * i;
  *k = 3 * i;
}


int x, y;
func(3, &x, &y);

I hope this helps to things for you.

I was wondering if it would be possible to attach the code file to this thread in addition to the thread on the FIRST forums? I don’t have access to an account on the FIRST forum, but I would like to take a look at the code as well.

Thanks-
DanDon

[EDIT]

Just set up an account on the FIRST forums. Contrary to my original assumptions, accounts are not only for the main team contacts, which is why I thought that I would not be able to have access to one. Thanks to Dave Scheck for letting me know.

[/EDIT]

THANK YOU!

That makes perfect sense.

We hosted a demo and presentation at Milford High School last Saturday, November 15. Here is a link to our First Forums post.

http://forums.usfirst.org/showthread.php?p=19678&

There you can find our C++ demo code for that day and links to our ppt presentations.

DaveD

In C++, you can also define a “reference” to an object. References allow you to use the same syntax that you use with the object (object.method()), but you get the same behavior as a pointer (not copied, etc). WPILib for C++ makes use of references and use of them is recommended over pointers.