简体   繁体   中英

How to use a C++ pointer of abstract type to point to a concrete class?

I'm working with the Point Cloud Library and I'm trying to avoid repeating the following behaviour:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr filter(PointCloud<pcl::PointXYZRGB>::Ptr input_cloud) {
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZRGB>);
    subclass.setInputCloud(input_cloud);
    subclass.filter(*cloud_filtered);
    return cloud_filtered;
}

I hoped to use something based on this example and along the lines of:

pcl::Filter<PointXYZRGB>* f;
pcl::Subclass<PointXYZRGB> s; //where s is an implementation of f
f = &s;

pcl::PointCloud<pcl::PointXYZRGB>::Ptr filter(PointCloud<pcl::PointXYZRGB>::Ptr input_cloud) {
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZRGB>);
    f->setInputCloud(input_cloud);
    f->filter(*cloud_filtered);
    return cloud_filtered;
}

However this will not compile as f does not name a type as reported by the compiler.

I assume is this due to pcl::Filter being an abstract class?

Is this approach valid for an example class such as pcl::VoxelGrid or is there an alternative?

Any help is much appreciated!

The line f = &s; should be moved to within a function.

In this case it was moved to within the constructors of derived subclasses.

Credit and thanks for the answer to user, aschepler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM