Template classes and ROS messages

download Template classes and ROS messages

If you can't read please download the document

Transcript of Template classes and ROS messages

-Eric Cote

TEMPLATE FUNTIONS/CLASSES IN C++ AND
LAB 1: INTRODUCTION TO ROS MESSAGE PASSING

Template Functions

-What if you need the same function for two different variable types?

void swap_values(char& v1, char& v2){char temp;

temp = v1;v1 = v2;v2 = temp;}

void swap_values(int& v1, int&v2){int temp;

temp = v1;v1 = v2;v2 = temp;}

templatevoid swap_values(T& v1, T& v2){T temp;

temp = v1;v1 = v2;v2 = temp;}

Instead, Let's Use a Template!

templatevoid swap_values(T& v1, T& v2){T temp;

temp = v1;v1 = v2;v2 = temp;}

(Type)

Type Parameter

Type Parameter to be replaced

templatevoid swap_values(T& v1, T& v2){T temp;temp = v1;v1 = v2;v2 = temp;}int main(){int integer1 = 1, integer 2 = 2;swap_values(integer1, integer2);cout