0

I have this code, I am looking at the code below, and I cannot figure out why on earth class Node; would be written and then again but with implementation.

class Doubly_Linked_List
{
   class Node; // <--- Why is this declared here??
   
   class Node : public Link {
        // Some Node code goes here.
   }
   
   Link head;
}
  • 11
    I suppose you didn't show us all the code and the reason for the forward declaration is in the code you did not post. In this snippet you can remove `class Node;` without effect – 463035818_is_not_an_ai Jan 20 '21 at 13:32
  • 1
    If you want to ask "why is it legal C++ syntax" instead, lookup "class declaration" (although it seems that you already know what it is, so what's the question?) – user202729 Jan 20 '21 at 13:33
  • 1
    maybe this can answer your question: https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c (answers are not considering the case of a nested class, but that doesn't make a big difference) – 463035818_is_not_an_ai Jan 20 '21 at 13:33
  • I am hestitant to flag as duplicate, because you ask "why?" and there is no reason in the posted code. As already mentioned, this might be because the code you posted is incomplete – 463035818_is_not_an_ai Jan 20 '21 at 13:35
  • 2
    I'm voting to reopen this question. There might be missing code from this question, but there might not. The question may literally be -- "Why declare a class immediately before defining it?" If that's the case, that may be a clearer title than "why is this declared here". – Drew Dormann Jan 20 '21 at 13:42
  • Does this answer your question? [When can I use a forward declaration?](https://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration) – Moia Jan 20 '21 at 17:26

1 Answers1

1

Declarations like class Node; are forward declarations. They are sometimes useful if you want to do some basic operations with your class, which don't require a full declaration.


One example of such a basic operation is declaration of methods. Suppose you have a bunch of very important methods in your Doubly_Linked_List class, and you want their declarations near the top. Because some methods mention Node in their declaration, you need some kind of declaration for Node - a forward declaration is good because it doesn't distract you from the really important stuff - your methods.

To define your methods, you have to provide a definition for Node, but you do it later in code, which helps organize it in a logical way - most important stuff first.

class Doubly_Linked_List
{
public:
   class Node; // forward declaration
   int very_important_method_1(Node node1, Node node2);
   Node very_important_method_2();

   // a virtual code separator; all code below is less important

   class Node : public Link {
        // Some Node code goes here.
   }
   
private:
   Link head;
}

int Doubly_Linked_List::very_important_method_1(Node node1, Node node2)
{
   // code
}

Doubly_Linked_List::Node Doubly_Linked_List::very_important_method_2()
{
   // code
}

This is a niche technique; very rarely useful if you define your class shortly after you forward-declare it.


Forward declarations are usually useful when the definition of your class is in another file.

You can separate your code into two files, interface and implementation. Definition of Node doesn't belong in the interface file (*.h), but it's needed to declare parts of Doubly_Linked_List class. This is the classic use of forward declarations.

// Doubly_Linked_List.h
class Doubly_Linked_List
{
public:
   class Node;
   int very_important_method_1(Node node1, Node node2);
   Node very_important_method_2();
   
   Node* whatever; // pointers to incomplete type are allowed here
}
// Doubly_Linked_List.cpp
class Node : public Link {
    // Some Node code goes here.
}

int Doubly_Linked_List::very_important_method_1(Node node1, Node node2)
{
   // code
}

Doubly_Linked_List::Node Doubly_Linked_List::very_important_method_2()
{
   // code
}
anatolyg
  • 26,506
  • 9
  • 60
  • 134