Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ea04dc

Browse files
add dot operator and arrow operator examples
1 parent 3acd0e9 commit 2ea04dc

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

‎map_new_key_custom_default_value.cpp‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#include <iostream>
22
#include <map>
33

4+
#ifdef PCL_INSTALLED
5+
#include <pcl/point_cloud.h>
6+
#include <pcl/point_types.h>
7+
#endif
8+
49
// https://stackoverflow.com/questions/2333728/stdmap-default-value
510
// https://www.learncpp.com/cpp-tutorial/overloading-the-assignment-operator/
611
// https://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm
712
// https://stackoverflow.com/questions/10744787/operator-must-take-exactly-one-argument
13+
// https://stackoverflow.com/questions/21569483/c-overloading-dereference-operators
814

915
struct IntWithDefaultValue {
1016
int i = -1;
@@ -29,6 +35,36 @@ struct StringWithDefaultValue {
2935
}
3036
};
3137

38+
#ifdef PCL_INSTALLED
39+
40+
struct CloudPtrDefaultedToEmpty {
41+
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud =
42+
pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
43+
operator pcl::PointCloud<pcl::PointXYZ>::Ptr() const { return cloud; }
44+
pcl::PointCloud<pcl::PointXYZ> & operator*() { return *cloud; }
45+
pcl::PointCloud<pcl::PointXYZ>::Ptr operator->() { return cloud; }
46+
pcl::PointCloud<pcl::PointXYZ>::Ptr &operator=(
47+
const pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud_in) {
48+
cloud = cloud_in;
49+
return cloud;
50+
}
51+
};
52+
53+
struct CloudIPtrDefaultedToEmpty {
54+
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud =
55+
pcl::PointCloud<pcl::PointXYZI>::Ptr(
56+
new pcl::PointCloud<pcl::PointXYZI>);
57+
operator pcl::PointCloud<pcl::PointXYZI>::Ptr() const { return cloud; }
58+
pcl::PointCloud<pcl::PointXYZI> & operator*() { return *cloud; }
59+
pcl::PointCloud<pcl::PointXYZI>::Ptr operator->() { return cloud; }
60+
pcl::PointCloud<pcl::PointXYZI>::Ptr &operator=(
61+
const pcl::PointCloud<pcl::PointXYZI>::Ptr &cloud_in) {
62+
cloud = cloud_in;
63+
return cloud;
64+
}
65+
};
66+
#endif
67+
3268
int main() {
3369
std::map<std::string, IntWithDefaultValue> intmap;
3470
std::cout << intmap["hello"] << std::endl; // -1
@@ -37,5 +73,33 @@ int main() {
3773

3874
std::map<std::string, StringWithDefaultValue> strmap;
3975
std::cout << strmap["a"] << std::endl; // defaultstring
76+
77+
#ifdef PCL_INSTALLED
78+
CloudPtrDefaultedToEmpty cloud1, cloud2;
79+
std::cout << cloud1->size() << std::endl;
80+
81+
for(size_t i = 0; i < 10; ++i) {
82+
cloud1->push_back(pcl::PointXYZ(1.0f * i, 2.0f * i, 3.0f * i));
83+
}
84+
std::cout << cloud1->size() << std::endl;
85+
86+
for(size_t i = 0; i < 10; ++i) {
87+
cloud2->push_back(pcl::PointXYZ(-1.0f * i, -2.0f * i, -3.0f * i));
88+
}
89+
std::cout << cloud2->size() << std::endl;
90+
91+
*cloud1 += *cloud2;
92+
std::cout << cloud1->size() << std::endl;
93+
#endif
4094
return 0;
4195
}
96+
97+
/*
98+
-1
99+
2
100+
defaultstring
101+
0
102+
10
103+
10
104+
20
105+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /