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
Post a Comment