首頁  >  問答  >  主體

c++ 变量类型判断

  1. 请问下面这个变量m是什么类型?NOTIFICATION 是一个 struct

class EventSensor;

EventSensor<NOTIFICATION> m;
PHP中文网PHP中文网2715 天前642

全部回覆(3)我來回復

  • PHPz

    PHPz2017-04-17 15:38:14

    如果EventSensor是一個類別模板​​(你給的聲明不是類別模板的聲明)。變數宣告EventSensor<NOTIFICATION> m;會宣告一個以NOTIFICATION為模板參數特化出的類別的實例m,這個類別稱為"EventSensor"(c++標準中便是如此稱呼的)。

    也就是說,變數m的型別就是EventSensor<NOTIFICATION>

    When template arguments are provided or, for function and class (since C++17) templates only, deduced, they are substituted for the template parameters to temptain a specialization of the template, that; a specific type or a specific function lvalue.

    引自cppreference, template

    回覆
    0
  • ringa_lee

    ringa_lee2017-04-17 15:38:14

    是的, 是class EventSensor內部的變數的型別,只不過在class 定義的時候是泛型,然後用NOTIFICATION實例化的。

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:38:14

    m 類型為 EventSensor
    是用於class內部的變數的型別,也就是m裡面某個變數的型別。

    ArrayList<String> mStrList = new ArrayList<String>();
    mStrList.add("string1");
    mStrList.add("string2");
    String s = mStrList.get(1);

    mStrList 的型別為ArrayList

    裡面的元素s=mStrList.get(1);的型別為String;

    回覆
    0
  • 取消回覆