简体   繁体   English

以下c ++代码的语法是什么?

[英]What's the syntax of following c++ code?

In following c++ code, what is the purpose of the LinkedList<Derived>:: ? 在下面的c ++代码中, LinkedList<Derived>::的目的是什么? I never see that syntax. 我从来没有看到这种语法。

template<class Derived, class List> LinkedListItem<Derived, List>::~LinkedListItem() {
    if(_list) _list->LinkedList<Derived>::cut(static_cast<Derived*>(this));
}

I try to search before asking, but just can not find anything. 我在询问之前尝试搜索,但却找不到任何东西。 Sorry if there is already a post on that. 对不起,如果已有帖子。

Say you have two classes: 假设您有两个班级:

struct A
{
   virtual void foo() {std::cout << "Came to A::foo()\n";}
};

struct B : A
{
   virtual void foo() {std::cout << "Came to B::foo()\n";}
};

and a pointer: 和一个指针:

A* ap = new B();

If you call 如果你打电话

ap->foo();

You will execute B::foo() . 您将执行B::foo() However, if you want to execute A::foo() on that pointer, you can use: 但是,如果要在该指针上执行A::foo() ,可以使用:

ap->A::foo();

Coming to your posted code, 来到你发布的代码,

If you add some white space and put things in multiple lines, the same code is: 如果添加一些空格并将内容放在多行中,则相同的代码为:

template<class Derived, class List>
LinkedListItem<Derived, List>::~LinkedListItem()
{
    if(_list) 
        _list->LinkedList<Derived>::cut(static_cast<Derived*>(this));
}

The line 这条线

template<class Derived, class List>

indicates that we are looking at a class template, a class template member function, or a function template. 表示我们正在查看类模板,类模板成员函数或函数模板。

The line 这条线

LinkedListItem<Derived, List>::~LinkedListItem()

indicates that we are looking at the destructor of the class template LinkedListItem . 表示我们正在查看类模板LinkedListItem的析构函数。

The lines 线条

    if(_list) 
        _list->LinkedList<Derived>::cut(static_cast<Derived*>(this));

indicate that LinkedListItem has a member variable _list . 表明LinkedListItem有一个成员变量_list If the member variable _list is not NULL, we are invoking some function on it. 如果成员变量_list不是NULL,我们就会调用它上面的一些函数。

The line 这条线

        _list->LinkedList<Derived>::cut(static_cast<Derived*>(this));

seems to indicate that the type of _list has a base class template called LinkedList that has a member function called cut . 似乎表明_list的类型有一个名为LinkedList的基类模板,它有一个名为cut的成员函数。 The line calls that function using an argument that is obtained by casting this to a Derived* . 该行使用通过将this转换为Derived*获得的参数来调用该函数。

LinkedList<Derived> is a type and it is also a scope . LinkedList<Derived>是一种类型,它也是一个scope :: is the scope operator which tells the compiler that the scope of cut(static_cast<Derived*>(this)) it resides (LinkedList). ::是作用域操作符,它告诉编译器它所在的cut(static_cast<Derived*>(this))范围cut(static_cast<Derived*>(this)) (LinkedList)。

The following is a likely structure : LinkedList<Derived> is a template class implemented a linked list of Derived objects. 以下是可能的结构: LinkedList<Derived>是实现Derived对象的链接列表的模板类。 It is composed to LinkedListItem<Derived, List> objects which wrap around a Derived* and maintain the other data components relevant to a linked list(next, prev pointers, head pointer, etc). 它由LinkedListItem<Derived, List>对象组成,它们包围Derived*并维护与链表相关的其他数据组件(下一个,prev指针,头指针等)。 Every LinkedListItem<Derived,List> object also contains a pointer to the LinkedList<Derived> object it is part of. 每个LinkedListItem<Derived,List>对象还包含指向它所属的LinkedList<Derived>对象的指针。 This is _list . 这是_list So, when you destroy a LinkedListItem , you also want to cut it from the original list. 因此,当您销毁LinkedListItem ,您还希望cut其从原始列表中cut cut will likely modify the contents of the neighbours of the LinkedListItem being deleted in order to maintain all the LinkedList invariants. cut可能会修改要删除的LinkedListItem的邻居的内容,以便维护所有LinkedList不变量。

LinkedList<Derived> in _list->LinkedList<Derived>::cut(static_cast<Derived*>(this)); LinkedList<Derived> in _list->LinkedList<Derived>::cut(static_cast<Derived*>(this)); means that the function cut belongs to LinkedList<Derived> template, as you can notice by the scope operator :: 表示函数cut属于LinkedList<Derived>模板,您可以通过范围运算符注意到::

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM