其他分享
首页 > 其他分享> > cartographer_common_configuration_file_resolver

cartographer_common_configuration_file_resolver

作者:互联网

class FileResolver 读lua参数文件程序接口

cartographer中大部分接口函数定义为纯虚函数,然后通过另一个子类实现。

如FileResolver类:

class FileResolver {
 public:
  virtual ~FileResolver() {}
  virtual std::string GetFullPathOrDie(const std::string& basename) = 0;
  virtual std::string GetFileContentOrDie(const std::string& basename) = 0;
};

实现方式:

// configuration_file_resolver.h
class ConfigurationFileResolver : public FileResolver {
public:
  explicit ConfigurationFileResolver(
      const std::vector<std::string>& configuration_files_directories);

  std::string GetFullPathOrDie(const std::string& basename) override;  //override 标记该函数为虚函数,必须定义,具体用法,参考小贴士。
  std::string GetFileContentOrDie(const std::string& basename) override;

 private:
  std::vector<std::string> configuration_files_directories_;
};

carto 代码引用说明:

ConfigurationFileResolver—> nodeOptions(carto节点参数配置)

[-] D:\cartographer\cartographer_ros-master\cartographer_ros-master\cartographer_ros\cartographer_ros\node_options.cc


小贴士:

  1. override 用法

    在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数。
    位置:函数调用运算符之后,函数体或纯虚函数标识 “= 0” 之前。

    不使用 override:

    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:
     virtual void foo() { cout << "Base::foo" << endl; }
     virtual void goo() { cout << "Base::goo" << endl; }
     // ...
    };
    
    class Derived : public Base
    {
    public:
     void foo() { cout << "Derived::foo" << endl; }
     void gao() { cout << "Derived::goo" << endl; } // 错误的将goo写成了gao,但编译器并不会给出提示
     // ...
    };
    
    int main(void)
    {
     Derived d;
     d.foo();                  // Derived::foo
     d.goo();                  // Base::goo 很明显,这不是我们想要的结果
    
     return 0;
    }

    使用 override:

    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:
        virtual void foo()
        {
            cout << "Base::foo()" << endl;
        }
    
        virtual void bar()
        {
            cout << "Base::bar()" << endl;
        }
    
        void goo()
        {
            cout << "Base::goo()" << endl;
        }
    };
    
    class Derived : public Base
    {
    public:
        void foo() override          // ok
        {
            cout << "Derived::foo()" << endl;
        }
    
        void foo() const override    // error: Derived::foo does not override. signature mismatch.
        {
            cout << "Derived::foo()" << endl;
        }
    
        void goo() override          // error: Base::goo is not virtual
        {
            cout << "Derived::goo()" << endl;
        }
    
        void bao() override          // error: 将bar误写成了bao,且基类中无名为bao的虚函数,
        {                            // 由于使用了override,编译器会检测出此错误
            cout << "Derived::bao()" << endl;
        }
    };

    在派生类的成员函数中使用override时,如果基类中无此函数,或基类中的函数并不是虚函数,编译器会给出相关错误信息。

主要内容来自:https://blog.csdn.net/linuxwuj/article/details/83183381

标签:std,cartographer,const,string,basename,resolver,override,configuration,函数
来源: https://www.cnblogs.com/heimazaifei/p/12435845.html