-5

Here is a code of a class of Linked List , i have a question on the function Singly_linked_list *GetNext().What does that means if a class name is stated before the function name? Is that a data type?Also,same question on the data member Singly_linked_list *nextPtr.I Please help Thank you

class Singly_linked_list // Use a class Singly_linked_list to represent an object{
public:
// constructor initialize the nextPtr
Singly_linked_list()
{
    nextPtr = 0; // point to null at the beginning
}

// get a number
int GetNum()
{
    return number;
}

// set a number
void SetNum(int num)
{
    number = num;
}

// get the next pointer
Singly_linked_list *GetNext()
{
    return nextPtr;
}

// set the next pointer
void SetNext(Singly_linked_list *ptr)
{
    nextPtr = ptr;
}

 private:
int number;
Singly_linked_list  *nextPtr;
 };
user007
  • 101
  • 2
  • 2
    You really need an [introductory book on C++](http://stackoverflow.com/q/388242/179910). Those are the return type of the functions in question. – Jerry Coffin Feb 02 '13 at 15:04
  • Nothing is being returned _to_ the class. What's being returned is an instance of the class (or a pointer to one). – Jerry Coffin Feb 02 '13 at 15:11

1 Answers1

4
// get the next pointer
Singly_linked_list *GetNext()
{
    return nextPtr;
}

This means the function GetNext returns a pointer to an instance of the class Singly_linked_list.

Likewise

Singly_linked_list  *nextPtr;

means nextPtr is a pointer to an instance of the class Singly_linked_list.

leemes
  • 44,967
  • 21
  • 135
  • 183
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • Edit: understanding how something can be of the type of a class was the main problem in the question -- emphasized that – leemes Feb 02 '13 at 15:16