Can we change the value of *y
in void function(const int*& x )
when y
(i.e int*y= new int
) is passed as an argument to function()
?
If anyone could put it in words it would be great. Please refer to the following code for a better comprehension of my question:
void DoWork(const int* &n)
{
*n = *n * 2; // will this change the value of *a?
}
int main()
{
int* a= new int;
DoWork(a);
}
I was trying to understand a program where I came across a similar syntax. Have a look at a snippet from that program:
void segmentTable(const pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud,
const pcl::PointCloud<pcl::PointXYZRGB>::Ptr& seg_cloud){
double z_min = -5, z_max = 0;
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
// Create the segmentation object
pcl::SACSegmentation<pcl::PointXYZRGB> seg;
// Optional
seg.setOptimizeCoefficients (true);
// Mandatory
seg.setModelType (pcl::SACMODEL_PLANE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setDistanceThreshold (0.01);
seg.setInputCloud (cloud);
seg.segment (*inliers, *coefficients);
// Project the model inliers
pcl::ProjectInliers<pcl::PointXYZRGB> proj;
proj.setModelType (pcl::SACMODEL_PLANE);
// proj.setIndices (inliers);
proj.setInputCloud (cloud);
proj.setModelCoefficients (coefficients);
proj.filter (*seg_cloud);
/* Create Convex Hull to segment everything above plane */
pcl::ConvexHull<pcl::PointXYZRGB> cHull;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_hull (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointIndices::Ptr hull_inliers (new pcl::PointIndices);
cHull.setInputCloud(seg_cloud);
cHull.reconstruct(*cloud_hull);
cHull.setDimension (2);
if (cHull.getDimension () == 2){
pcl::ExtractPolygonalPrismData<pcl::PointXYZRGB> prism;
prism.setInputCloud (cloud);
prism.setInputPlanarHull (cloud_hull);
prism.setHeightLimits (z_min, z_max);
prism.setHeightLimits (z_min, z_max);
prism.segment (*hull_inliers);
}
pcl::ExtractIndices<pcl::PointXYZRGB> extract_indices;
extract_indices.setInputCloud(cloud);
extract_indices.setIndices(hull_inliers);
extract_indices.setNegative(true);
extract_indices.filter(*cloud);
}
void int main(){pcl::PointCloud<pcl::PointXYZRGB>::Ptr temp_cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::fromPCLPointCloud2(pcl_pc2,*temp_cloud);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr seg_cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
passThroughFilter(temp_cloud);
segmentTable(temp_cloud, seg_cloud);}
This code does change the values in temp_cloud and seg_cloud.
CodePudding user response:
No, you are not allowed to change *n
since it's a const int
. You need to make it non-const
for it to work:
void DoWork(int*& n)
{
*n = *n * 2;
}
Also note that *a
is uninitialized so reading it would make the program have undefined behavior. You need to initialize it:
int main()
{
int* a = new int{1}; // *a is now initialized to 1
DoWork(a);
delete a; // and delete
}
The global int a;
that you added isn't involved here. The local a
in main
shadows the global a
in main
and it's that local a
that is passed as an argument to DoWork
.
The code in the added block doesn't do the same thing. In your added code block the value Ptr
is pointing at is non-const
so there the value can be changed. Consider this:
using Ptr = int*;
void foo(const Ptr& p) { // p = int* const&
*p *= 2; // ok, *p is an int, not a const int
// p = nullptr; // error, the pointer is const
}
int main() {
int* a = new int{1};
foo(a);
delete a;
}