Help with C++ code

Hi, i am new to C++, i have done a couple of tutorials and know the basics(variables,func, class etc), but still need to learn in-depth concepts.

In the code below, can someone explain the concepts that are used, like say constructors, namespaces, etc, so that i can lean those topics in detail from online sources. You can skip the bare basics like variables, func etc, that any beginer would be familiar with.

I am at a stage where i don’t know what i don’t know (if you know what i mean :slight_smile: ). Any help would be greatly appreciated. Ideally ,when answering,use image to mention the code you are talking about for better readability.

Code 1:

#include <gazebo/gazebo.hh>

namespace gazebo
  class WorldPluginTutorial : public WorldPlugin
    public: WorldPluginTutorial() : WorldPlugin()
              printf("Hello World!\n");

    public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)

Code 2:

#include <thread>
#include "ros/ros.h"
#include "ros/callback_queue.h"
#include "ros/subscribe_options.h"
#include "std_msgs/Float32.h"

/// \brief A node use for ROS transport
private: std::unique_ptr<ros::NodeHandle> rosNode;

/// \brief A ROS subscriber
private: ros::Subscriber rosSub;

/// \brief A ROS callbackqueue that helps process messages
private: ros::CallbackQueue rosQueue;

/// \brief A thread the keeps running the rosQueue
private: std::thread rosQueueThread;

// Initialize ros, if it has not already bee initialized.
if (!ros::isInitialized())
  int argc = 0;
  char **argv = NULL;
  ros::init(argc, argv, "gazebo_client",

// Create our ROS node. This acts in a similar manner to
// the Gazebo node
this->rosNode.reset(new ros::NodeHandle("gazebo_client"));

// Create a named topic, and subscribe to it.
ros::SubscribeOptions so =
      "/" + this->model->GetName() + "/vel_cmd",
      boost::bind(&VelodynePlugin::OnRosMsg, this, _1),
      ros::VoidPtr(), &this->rosQueue);
this->rosSub = this->rosNode->subscribe(so);

// Spin up the queue helper thread.
this->rosQueueThread =
  std::thread(std::bind(&VelodynePlugin::QueueThread, this));
/// \brief Handle an incoming message from ROS
/// \param[in] _msg A float value that is used to set the velocity
/// of the Velodyne.
public: void OnRosMsg(const std_msgs::Float32ConstPtr &_msg)

/// \brief ROS helper function that processes messages
private: void QueueThread()
  static const double timeout = 0.01;
  while (this->rosNode->ok())

Hi @Joseph1001,

I don’t know whether you are taking the C++ for Robotics course, but if not, you should.

The code below works in the same way as python imports. You are including a header that defines specific classes:

Below, a namespace is defined. Namespaces are basically to avoid conflicts because the same WorldPluginTutorial class may have been defined in different files and the compiler wouldn’t know which class you are referring to if you have two classes with the same name. By using namespace, we tell the compiler the exact class we want, the one under the namespace gazebo.

Imagine I say: “Come here, Joseph”, but there are two Josephs nearby. I have to specify, give it a namespace: namespace india { class Joseph{}; }

Below we have an inheritance. WorldPluginTutorial inherits methods from WorldPlugin.

Below are things specific to Gazebo, to allow Gazebo to load your plugin.

For Code 2, I think you didn’t put the whole code, because the code below should be inside a class, I suppose:

To understand public, protected and private, see C++ Access Specifiers

In the code below, ros is the namespace, Subscriber is the class, and rosSub is the variable.

!” is the same as “not” in Python.

In the code below, we have a pointer of pointer (one of the most complicated thing for beginners). See

For the code below:

  • Namespaces are accessed with: namespace::ClassName
  • Instance attributes are accessed with: myObject.methodName()
  • If myObject is a pointer instead of a normal variable, you would access attribues with: myObject->methodName()

Thanks @ralves for clarifying.

I have taken the C++ course from Construct, but felt that the course required additional explanations on certain topics, epecially for a beginner like me. i had mentioned in a previous post all the things that could be improved in that course Clarification on the content of C++ for Robotics Basics. I think new users, especailly ones that are new to C++ will benefit if the queries mentioned in the post was answered in the course itself.

Thank you very much for your feedback, @Joseph1001

I’m taking notes of your suggestions, so that we may improve the course as soon as we can.

1 Like

This topic was automatically closed after 7 days. New replies are no longer allowed.