Please can someone help explain this code

I am working though a book and found two confusing things. This is for the implementation of a linked list. There are two classes; one for the node and one for the list maintenance.

When a node is added using one of the member functions of the list maint class, it calls the constructor for the node class which has the following definition.

IntNode(in el, IntNode *ptr = 0)
{
info = el;
next = ptr;
}

"info" and "next" are both public members of IntNode.

The calling line looks like this:

head = new IntNode(el,head)

"head" is a member of the list main class

What is confusing me is this. When the constructor is called with the el, head parameters, does the value of head not get initialised to 0 in the definition of the IntNode constructor. Therefore the value assigned to next in the body of IntNode constructor should also be 0. I know this is not the case because I have run the code and it works fine, it just that I don't understand this declaration. Perhaps this is just following good coding practice by initialising the ptr variable to 0, but the parameter passed in still maintains its value and the parameter is only assigned the value IntoNode is called with after this deceleration.


The second thing that I find confusing me is that there is also a function declared like this:

bool IntSList::IntSList(int el) const {.....

What is the purpose of the const after the declaration?

I hope this explanation makes sense to all the C++ gurus out there, please excuse the colloquial explanation.

Thanks
Rob
When the constructor is called with the el, head parameters, does the value of head not get initialised to 0 in the definition of the IntNode constructor.



It's easiest for me to explain this by example.

1
2
3
head = 0;
head = new IntNode(1,head); // node1
head = new IntNode(2,head); // node2 


Here, the following things happen:

line 1:
- head is null (0)

line 2:
- new node is created (node1).
- node1->next is set to 'head', since 'head' is null, this means node1->next is also null
- head now points to node1

line 3:
- new node created (node2)
- node2->next is set to 'head'. 'head' points to node1, so node2->next also points to node1
- head now points to node2


so we're left with:
head = &node2
node2->next = &node1
node1->next = null


What is the purpose of the const after the declaration?


It marks the function as a const member function, meaning you can call it with a const object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Foo
{
public:
  void f_const() const {}
  void f_nonconst() { }
};

int main()
{
  const Foo c; // const object
  Foo nc; // nonconst object

  nc.f_const();  // OK
  nc.f_nonconst();  // OK

  c.f_const(); // OK
  c.f_nonconst();  // ERROR, a const object can't call a non-const function 


The reason that isn't allowed is because const objects are "guaranteed" that you won't change them, but a non-const function might change the state of the object. So by designating the function as const, you are saying "it is guaranteed that this function does not change the state of the object), thereby making it OK to call that function with const objects.
Hello Disch, thanks for the response.

I think that what is blowing my mind is the decelration of the constructor "IntNode(in el, IntNode *ptr = 0)". I am reading this as follows:

The second paramter passed to the constrcutor is effectively being cancelled and nullified due the fact that *ptr is being set to 0 (NULL) at this point.

I can clearly see your explanation working when I run the code, but the mechanics of this one statement (i.e. IntNode *ptr = 0) is what is confusing me.

To help me to understand this issue; if I made the statement that the value passed in (and contained in the argument) is only actually assigned to the variable ptr after the decelration, would that be correct? If this statement is correct then "I see the light" (so to speak). If not, I am still confused.

regards
Rob
closed account (o3hC5Di1)
Hi there,

I'm no expert at this, but here's my 2 cents:

IntNode(int el, IntNode *ptr = 0)

In the second argument this takes the argument is not only declared, but also assigned a value, in this case 0.
Note that this argument is a pointer to a variable of type IntNode, not an actual variable itself.

Doing this means that if you create an object like this: object = IntNode(el);, your "next" property will be set to 0, because that is the default value assigned to the argument. If you do specify an argument like so object = IntNode(el, ptr);, the default value (0) will be discarded and your pointer assigned instead.

Hope that helps.

All the best,
NwN
This statement

head = new IntNode(el,head)

is valid only if head was initialized before it and has a valid value.

I can suppose that it is possible that a constructor of the list maint class initializes head to 0 (or NULL ). Or the first call to the function has the following record

head = new IntNode(el );
Last edited on
Topic archived. No new replies allowed.