-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; }
template<class T> void swap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } Instead, Let's Use a Template!
template<class T> void swap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } (Type)   Type Parameter Type Parameter to be replaced
template<class T> void 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 << “integer1 now equals ”  << integer1 << endl; cout << “integer2 now equals ” << integer2 << endl; return 0; }
Template Classes: A Brief Class Refresher class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; };
class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; }; Class Name Public Member Function Private Variables and Member Functions
class Robot { … } void Robot::funktion2(); { ... } Scope Resolution Operator
class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; }; class Robot { public: void funktion1(); private: void funktion2(); char v1; char v2; }; What if We Want?
Time for a Template Class template<class T> class Robot { public: void funktion1(T v1, T v2); private: void funktion2(); T v1; T v2; };
template<class T> class Robot { ... }; template<class T> void Robot<T>::funktion1(T v1, T v2); { v1 = v2; } Member Functions Are Also Templates Scope Resolution Operator
Do All the Variables in the Class Need to be of Type T? template<class T> class Robot { public: void funktion1(int v1, T v2); private: void funktion2(); int v1; T v2; }; Data Types to be Given by User
Using the Template Function int main() { Robot<char> robot1; Robot<double> robot2; }
Template Functions in Lab 1 n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); n.subscribe(&quot;chatter&quot;, 1000, chatterCallback);
In talker.cpp ros::Publisher chatter_pub = n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); #1 A Template Function! Scope Resolution Operator
n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); Parameter 1 Parameter 2 Scope Resolution Operator
ros::Subscriber chatter_sub = n.subscribe(&quot;chatter&quot;, 1000, chatterCallback); In listener.cpp Another Template Function! Why no <type>?
n.subscribe(&quot;chatter&quot;, 1000, chatterCallback); Parameter 1 Parameter 2
Other Things to Know for Lab1 -The dot operator object.memberfunction -Pointer operations -> and char** object * 0; (*0).dosomething; == 0->dosomething; -Unary ++ operator -argc and argv
Acknowledgements  -Professor Main -Problem Solving with C++  by Walter Savitch—The template function example came from this book. -ROS.org -Sam
Questions?

Template classes and ROS messages

  • 1.
    -Eric Cote TEMPLATEFUNTIONS/CLASSES IN C++ AND LAB 1: INTRODUCTION TO ROS MESSAGE PASSING
  • 2.
    Template Functions -Whatif you need the same function for two different variable types?
  • 3.
    void swap_values(char& v1,char& v2) { char temp; temp = v1; v1 = v2; v2 = temp; }
  • 4.
    void swap_values(int& v1,int&v2) { int temp; temp = v1; v1 = v2; v2 = temp; }
  • 5.
    template<class T> voidswap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } Instead, Let's Use a Template!
  • 6.
    template<class T> voidswap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } (Type) Type Parameter Type Parameter to be replaced
  • 7.
    template<class T> voidswap_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 << “integer1 now equals ” << integer1 << endl; cout << “integer2 now equals ” << integer2 << endl; return 0; }
  • 8.
    Template Classes: ABrief Class Refresher class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; };
  • 9.
    class Robot {public: void funktion1(); private: void funktion2(); int v1; int v2; }; Class Name Public Member Function Private Variables and Member Functions
  • 10.
    class Robot {… } void Robot::funktion2(); { ... } Scope Resolution Operator
  • 11.
    class Robot {public: void funktion1(); private: void funktion2(); int v1; int v2; }; class Robot { public: void funktion1(); private: void funktion2(); char v1; char v2; }; What if We Want?
  • 12.
    Time for aTemplate Class template<class T> class Robot { public: void funktion1(T v1, T v2); private: void funktion2(); T v1; T v2; };
  • 13.
    template<class T> classRobot { ... }; template<class T> void Robot<T>::funktion1(T v1, T v2); { v1 = v2; } Member Functions Are Also Templates Scope Resolution Operator
  • 14.
    Do All theVariables in the Class Need to be of Type T? template<class T> class Robot { public: void funktion1(int v1, T v2); private: void funktion2(); int v1; T v2; }; Data Types to be Given by User
  • 15.
    Using the TemplateFunction int main() { Robot<char> robot1; Robot<double> robot2; }
  • 16.
    Template Functions inLab 1 n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); n.subscribe(&quot;chatter&quot;, 1000, chatterCallback);
  • 17.
    In talker.cpp ros::Publisherchatter_pub = n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); #1 A Template Function! Scope Resolution Operator
  • 18.
  • 19.
    ros::Subscriber chatter_sub =n.subscribe(&quot;chatter&quot;, 1000, chatterCallback); In listener.cpp Another Template Function! Why no <type>?
  • 20.
  • 21.
    Other Things toKnow for Lab1 -The dot operator object.memberfunction -Pointer operations -> and char** object * 0; (*0).dosomething; == 0->dosomething; -Unary ++ operator -argc and argv
  • 22.
    Acknowledgements -ProfessorMain -Problem Solving with C++ by Walter Savitch—The template function example came from this book. -ROS.org -Sam
  • 23.