c++ - Can I use a variable in destructor that is not defined in the header file? -


i have been reading code 1 of class. , got confused on 1 of variables in destructor. here code, btw, linked list header file. , variable think not defined used n;

#include <iostream>  using namespace std;   class linkedlist { private:    struct node   {      int info;      node * next;   };    typedef node * nodeptr;    nodeptr start;    int count;   public:         // constructor     linkedlist()    {       start = null;       count = 0;    }         // destructor     ~linkedlist()    {       nodeptr p = start, n;        while (p != null)       {          n = p;          p = p->next;          delete n;       }    } 

the first time n showed line
nodeptr p = start, n; please teach me why legal use variable this. commands appreciated.

thank

in c++ language (just in c) each declaration can include multiple declarators. e.g.

int a, b, c; 

declares (and defines) 3 variables: a, b , c of type int. can optionally add initializers declarators (or of them)

int = 5, b, c; 

your

nodeptr p = start, n; 

is same thing. declares (and defines) 2 variables: p , n. p has initializer, n doesn't. so, assertion n "not defined" not true.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -