1. 程式人生 > 其它 >Eclipse 常用快捷鍵

Eclipse 常用快捷鍵

虛擬函式:多型性質,執行時編譯

虛擬函式的實現:虛擬函式指標和虛擬函式表

虛擬函式指標 (virtual function pointer) 從本質上來說就只是一個指向函式的指標,與普通的指標並無區別。它指向使用者所定義的虛擬函式,具體是在子類裡的實現,當子類呼叫虛擬函式的時候,實際上是通過呼叫該虛擬函式指標從而找到介面。

虛擬函式指標是確實存在的資料型別,在一個被例項化的物件中,它總是被存放在該物件的地址首位,這種做法的目的是為了保證執行的快速性。與物件的成員不同,虛擬函式指標對外部是完全不可見的,除非通過直接訪問地址的做法或者在DEBUG模式中,否則它是不可見的也不能被外界呼叫。

------文字轉自部落格https://blog.csdn.net/weixin_43329614/article/details/89103574

著重理解虛擬函式表,例項化的類物件共用一個虛擬函式表。空物件會有一個虛擬函式指標。另外,子類和父類的虛擬函式需一模一樣,不然就只能算過載,子類並不能覆蓋父類虛擬函式來實現多型。

侯捷《C++最佳程式設計實踐》視訊看講解。

虛解構函式:只有當一個類被定義為基類的時候,才會把解構函式寫成虛解構函式。保證基類指標被正確釋放,不加virtual有記憶體洩漏風險。

在看AMCL時,用到虛擬函式,整理下

1、感測器類

// Base class for all AMCL sensors
class AMCLSensor
{
  // Default constructor
  public: AMCLSensor();
         
  
// Default destructor public: virtual ~AMCLSensor(); // Update the filter based on the action model. Returns true if the filter // has been updated. public: virtual bool UpdateAction(pf_t *pf, AMCLSensorData *data); // Initialize the filter based on the sensor model. Returns true if the // filter has been initialized.
public: virtual bool InitSensor(pf_t *pf, AMCLSensorData *data); // Update the filter based on the sensor model. Returns true if the // filter has been updated. public: virtual bool UpdateSensor(pf_t *pf, AMCLSensorData *data); // Flag is true if this is the action sensor public: bool is_action; // Action pose (action sensors only) public: pf_vector_t pose; // AMCL Base //protected: AdaptiveMCL & AMCL; #ifdef INCLUDE_RTKGUI // Setup the GUI public: virtual void SetupGUI(rtk_canvas_t *canvas, rtk_fig_t *robot_fig); // Finalize the GUI public: virtual void ShutdownGUI(rtk_canvas_t *canvas, rtk_fig_t *robot_fig); // Draw sensor data public: virtual void UpdateGUI(rtk_canvas_t *canvas, rtk_fig_t *robot_fig, AMCLSensorData *data); #endif };

繼承類有兩個

class AMCLLaser : public AMCLSensor
class AMCLOdom : public AMCLSensor

2、感測器資料類

// Base class for all AMCL sensor measurements
class AMCLSensorData
{
  // Pointer to sensor that generated the data
  public: AMCLSensor *sensor;
          virtual ~AMCLSensorData() {}

  // Data timestamp
  public: double time;
};

繼承類也有兩個

class AMCLLaserData : public AMCLSensorData
class AMCLOdomData : public AMCLSensorData