Upcoming SlideShare
×

# Template classes and ROS messages

1,363 views
1,276 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,363
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Template classes and ROS messages

1. 1. -Eric Cote TEMPLATE FUNTIONS/CLASSES IN C++ AND LAB 1: INTRODUCTION TO ROS MESSAGE PASSING
2. 2. Template Functions -What if you need the same function for two different variable types?
3. 3. void swap_values(char& v1, char& v2) { char temp; temp = v1; v1 = v2; v2 = temp; }
4. 4. void swap_values(int& v1, int&v2) { int temp; temp = v1; v1 = v2; v2 = temp; }
5. 5. template<class T> void swap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } Instead, Let's Use a Template!
6. 6. 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
7. 7. 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; }
8. 8. Template Classes: A Brief Class Refresher class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; };
9. 9. class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; }; Class Name Public Member Function Private Variables and Member Functions
10. 10. class Robot { … } void Robot::funktion2(); { ... } Scope Resolution Operator
11. 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. 12. Time for a Template Class template<class T> class Robot { public: void funktion1(T v1, T v2); private: void funktion2(); T v1; T v2; };
13. 13. 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
14. 14. 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
15. 15. Using the Template Function int main() { Robot<char> robot1; Robot<double> robot2; }
16. 16. Template Functions in Lab 1 n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); n.subscribe(&quot;chatter&quot;, 1000, chatterCallback);
17. 17. In talker.cpp ros::Publisher chatter_pub = n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); #1 A Template Function! Scope Resolution Operator
18. 18. n.advertise<std_msgs::String>(&quot;chatter&quot;, 1000); Parameter 1 Parameter 2 Scope Resolution Operator
19. 19. ros::Subscriber chatter_sub = n.subscribe(&quot;chatter&quot;, 1000, chatterCallback); In listener.cpp Another Template Function! Why no <type>?
20. 20. n.subscribe(&quot;chatter&quot;, 1000, chatterCallback); Parameter 1 Parameter 2
21. 21. 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
22. 22. Acknowledgements -Professor Main -Problem Solving with C++ by Walter Savitch—The template function example came from this book. -ROS.org -Sam
23. 23. Questions?