user3771959
user3771959

Reputation: 43

What's the syntax of following c++ code?

In following c++ code, what is the purpose of the 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.

Upvotes: 3

Views: 96

Answers (4)

Saulo Ricci
Saulo Ricci

Reputation: 774

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 ::

Upvotes: 0

R Sahu
R Sahu

Reputation: 206557

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(). However, if you want to execute A::foo() on that pointer, you can use:

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.

The lines

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

indicate that LinkedListItem has a member variable _list. If the member variable _list is not NULL, we are invoking some function on it.

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. The line calls that function using an argument that is obtained by casting this to a Derived*.

Upvotes: 4

Pradhan
Pradhan

Reputation: 16737

The following is a likely structure : LinkedList<Derived> is a template class implemented a linked list of Derived objects. 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). Every LinkedListItem<Derived,List> object also contains a pointer to the LinkedList<Derived> object it is part of. This is _list. So, when you destroy a LinkedListItem, you also want to cut it from the original list. cut will likely modify the contents of the neighbours of the LinkedListItem being deleted in order to maintain all the LinkedList invariants.

Upvotes: 0

CS Pei
CS Pei

Reputation: 11047

LinkedList<Derived> is a type and it is also a scope. :: is the scope operator which tells the compiler that the scope of cut(static_cast<Derived*>(this)) it resides (LinkedList).

Upvotes: 0

Related Questions