【问题标题】:Declaring a pointer with a scope resolution operator in the name使用名称中的范围解析运算符声明指针
【发布时间】:2012-09-20 13:53:00
【问题描述】:

我试图在双向链表上理解本教程。 http://www.dreamincode.net/forums/topic/53161-how-to-create-a-basic-double-linked-list/

这部分我看不懂

  element* list::FirstEl = NULL;    //This initialises the static element* 'FirstEl' to  NULL  
  element* list::LastEl = NULL;     //This initialises the static element* 'LastEl' to  

我会说这段代码创建了一个名为“list::FirstEl”的元素类型的指针并将其设置为空,但范围解析运算符让我觉得有些东西我还没有在 C++ 中涵盖。

这是怎么回事?

【问题讨论】:

  • 像大多数 c 一样,你从右到左阅读它:在这个语句 (;) 中,值 'Null' 被分配 (=) 给作为成员变量的 'FirstEl' (:: ) 的对象“列表”,它是一个指针 (*),指向“元素”类型的对象。

标签: c++


【解决方案1】:

这是static 类成员初始化。

class list
{
    static element* FirstEl;
    static element* LastEl;
};

你所拥有的是成员的初始化。

变量的名称是FirstElLastEl,但它们是类的一部分,这就是为什么在定义它们时必须限定它们的名称。

就像在定义方法名称时限定它们一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2012-04-20
    • 2010-09-09
    • 1970-01-01
    • 2015-12-18
    • 2012-01-30
    • 2020-12-12
    相关资源
    最近更新 更多