Skip to content

Some C++ Concepts

1.Namespace in C++

#include <iostream>

namespace name_1{
    namespace name_2{
        int x;
        void my_fn(){
            std::cout<<"hi"<<std::endl;
        }
    }
}


int main() {

    //access
    name_1::name_2::x = 1;
    name_1::name_2::my_fn();

    return 0;

}

similar you will a lot find in gazebo sim codes

namespace gz
{
namespace sim
{
namespace systems
{


.....

}
}
}

//access outside
gz::sim::systems xyz; //means its inside gz , sim , systems namespace

so line like these gz::transport::Node node; means node in gz , transport namespace

also means in the doc its under gz -> transport in the gazebo sim transport api doc gz_transport





2.std::optional

The term std::option refers to the std::optional class template in C++, which is used to represent a value that may or may not be present.

std::optional<std::string> maybe_name = "Alice";

if (maybe_name.has_value()) {  //has_value()
    std::cout << "Name is: " << maybe_name.value() << std::endl;  //value()
}

so similarly in gazebo sim api also api example

optional

    auto entityOpt = _ecm.EntityByName(this->modelName);
    if (!entityOpt.has_value())
    {
      gzdbg << "Model [" << this->modelName
            << "] not found yet. Skipping velocity application." << std::endl;
      return;
    }

    this->targetEntity = entityOpt.value();
    gzmsg << "Found target model entity: " << this->targetEntity << std::endl;





3.this pointer

#include <iostream>

class person{
  private:
    int id;

  public:
   person(int id){
     this->id = id;  // access data member
                     // to avoid confusion id = id  
   }

   int show_id(){
      return this->id; 
   }

   void print_data(){
       std::cout<< this->show_id() << std::endl;  // accessing member funtion
   }
};


int main() {
    person a = person(5);
    a.print_data();
    return 0;
}

similary, on plugin code many time we use this pointer
this_pointer