Question on dynamically allocated memory

here is the code in question (from Stephan Prata's C++ Primer Plus).

my question is, for this line of code (line 24):
struct Node { Item item; struct Node * next;};

Is it necessary to use struct Node * next; (underlined) ?

My question is: Why can't one use only Node * next;, since Node was already declared as a struct? So the code would be like this:
struct Node { Item item; Node * next;};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// queue.h -- interface for a queue
#ifndef QUEUE_H_
#define QUEUE_H_
// This queue will contain Customer items
class Customer
{
private:
    long arrive;        // arrival time for customer
    int processtime;    // processing time for customer
public:
    Customer() : arrive(0), processtime (0){}
    void set(long when);
    long when() const { return arrive; }
    int ptime() const { return processtime; }
};

typedef Customer Item;

class Queue
{
private:
// class scope definitions
    // Node is a nested structure definition local to this class
    struct Node { Item item; struct Node * next;};
    enum {Q_SIZE = 10};			//constent for the array size. Type enumeration
// private class members
    Node * front;       // pointer to front of Queue
    Node * rear;        // pointer to rear of Queue
    int items;          // current number of items in Queue
    const int qsize;    // maximum number of items in Queue
    // preemptive definitions to prevent public copying
    Queue(const Queue & q) : qsize(0) { }
    Queue & operator=(const Queue & q) { return *this;}
public:
    Queue(int qs = Q_SIZE); // create queue with a qs limit. This is the constructor.
    ~Queue();				//destructor
    bool isempty() const;
    bool isfull() const;
    int queuecount() const;
    bool enqueue(const Item &item); // add item to end
    bool dequeue(Item &item);       // remove item from front
};
#endif 
Last edited on
I am going to cautiously guess that it is a remnant of C structs, where a variable is declared like:
1
2
3
struct myStruct{};
//...
struct myStruct variable;


But maybe someone can correct me.
@Daleth:
you are correct, C language used this form of declaring structure instants, C++ supports it for compatibility with C code, no more/no less.

i think it is actually considered obsolete i modern C++ programs, you must avoid it.
@programgirl

here is the code in question (from Stephan Prata's C++ Primer Plus).

my question is, for this line of code (line 24):
struct Node { Item item; struct Node * next;};

Is it necessary to use struct Node * next; (underlined) ?


No there is no need to use keyword struct before Node *. However it makes the code more readable and clear. Take into account that sometimes structures can be very big that their whole definitions do not fit one screen. So when you meet a line as struct Node * it gives you additional information about what is the Node.
Last edited on
> However it makes the code more readable and clear. Take into account that sometimes
> structures can be very big that their whole definitions do not fit one screen.
> So when you meet a line as struct Node * it gives you additional information about what is the Node.

Utter rubbish.

Even a barely competent C++ programmer is always aware of the current declarative region - both while writing code and while reading code.

An elaborated-type-specifier is required if and only if a class name is declared in a scope where a variable, function, or enumerator of the same name is also declared.

1
2
3
4
5
6
7
8
9
constexpr int A = 9 ;

class A
{
     // ...
     A* next = nullptr ; // within the declarative region of the class
}; 

class A object ; // elaborated-type-specifier is required 

Topic archived. No new replies allowed.