Home  >  Q&A  >  body text

c++ 变量类型判断

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

class EventSensor;

EventSensor<NOTIFICATION> m;
PHP中文网PHP中文网2765 days ago671

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-17 15:38:14

    If EventSensor is a class template (the declaration you gave is not a declaration of a class template). The variable declaration EventSensor<NOTIFICATION> m; will declare an instance m of a class specialized with NOTIFICATION as the template parameter. This class is called "EventSensor<NOTIFICATION>" (this is how it is called in the C++ standard).

    In other words, the type of variable m is 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 obtain a specialization of the template, that is, a specific type or a specific function lvalue.

    Quoted from cppreference, template

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 15:38:14

    Yes, <NOTIFICATION> is the type of variable inside class EventSensor, but it is generic when the class is defined and then instantiated with NOTIFICATION.

    reply
    0
  • 伊谢尔伦

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

    m is of type EventSensor
    <NOTIFICATION> which is the type of variables used inside the class, that is, the type of a variable in m.
    as

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

    mStrList is of type ArrayList

    The type of element s=mStrList.get(1); in

    is String;

    reply
    0
  • Cancelreply