How to Subclass and Reimplement a Class Contained in a Namespace?

I am trying to use PCLVisualizer to visualize a point cloud.

I need use the point picking event to get the z,y,z coordinates of the point that I selected when left clicking with the mouse on the point cloud in the visualizer:

The problem is that the point_picking_event.cpp code requires that the SHIFT key is pressed. This is keyboardless system (running Qt) so there is no way to select SHIFT key.

My plan is to inherit the class

pcl::visualization::PointPickingCallback

and change the code in pcl::visualization::PointPickingCallback::Execute that checks the SHIFT key

if ((eventid == vtkCommand::LeftButtonPressEvent) && (iren->GetShiftKey () > 0))

to

 if (eventid == vtkCommand::LeftButtonPressEvent)

Then I would place the inherited class in its own namespace mynamespace and call it something like inhPointPickingCallback

The new function

mynamespace::inhPointPickingCallback::Execute

would now just not check the SHIFT key and we should be good.

QUESTION: I cannot wrap my head around how to inherit and redefine this class. (I am taking my C++ from a starting to a more advanced level).

Can you help me with a compiling example with a header (.h) file and .cpp file that inherits pcl::visualization::PointPickingCallback and reimplements the function pcl::visualization::PointPickingCallback::Execute (...)

in its own namespace and class mynamespace::inhPointPickingCallback::Execute (...) with the code changed to not check the SHIFT key?

1

1 Answer

This example should get you startet with the inheritance and namespaces. Its straigth forward just declare your new class in the namespace you want and derive from it. The code is quite easy and you should not have any problem to split it up to header and cpp.

#include <iostream>

namespace pcl{
namespace visualization {
     class PointPickingCallback{
         public:
         PointPickingCallback(){
             std::cout << "Contruct PointPickingCallback" << std::endl;
         }
         virtual void Execute(){
             std::cout << "With shift" << std::endl;
         }
     };
}
}

namespace mynamespace{
    class InhPointPickingCallback : public pcl::visualization::PointPickingCallback {
        public:
        InhPointPickingCallback(){
            std::cout << "Contruct Inherited PPC" << std::endl;
        }
        void Execute() override{ //note override is c++11
            std::cout << "Without shift" << std::endl;
        }
    };
}

int main()
{
    //without inheritance
    //pcl::visualization::PointPickingCallback ppc;
    mynamespace::InhPointPickingCallback ppc;
    ppc.Execute();
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest