11

I'm studying data structures (List, Stack, Queue), and this part of code is confusing me.

ListNode( const Object& theElement = Object(), ListNode * node = NULL);


template<class Object>
ListNode<Object>::ListNode( const Object& theElement, ListNode<Object> * node) {
    element = theElement;
    next = node;
}
  1. Why there are assignment operators within function parameters?
  2. What does Object() call do?
Harry Cho
  • 2,309
  • 4
  • 20
  • 28

3 Answers3

19

Those are not assignment operators. Those are default arguments for the function.

A function can have one or more default arguments, meaning that if, at the calling point, no argument is provided, the default is used.

void foo(int x = 10) { std::cout << x << std::endl; }

int main()
{
  foo(5); // will print 5
  foo(); // will print 10, because no argument was provided
}

In the example code you posted, the ListNode constructor has two parameters with default arguments. The first default argument is Object(), which simply calls the default constructor for Object. This means that if no Object instance is passed to the ListNode constructor, a default of Object() will be used, which just means a default-constructed Object.

See also:
Advantage of using default function parameter
Default value of function parameter

M.M
  • 138,810
  • 21
  • 208
  • 365
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • I'm tempted to up-vote if you would mention that default parameters are one of the more "evil" C++ features. They turn innocent constructors in converting constructors or default constructors. They make it hard to `bind` functions. The are a hindrance when only some parameters should be specified and others left to default. – pmr Apr 11 '12 at 10:56
  • Me neither. One size does not fit all. Sometimes they are very useful. – Spook Apr 11 '12 at 11:00
  • @pmr, I don't necessarily agree with you that they are "evil". They are a useful way to avoid repeating yourself by writing multiple constructors, when you can simply provide one constructor with a default parameter. The standard library implementations often take advantage of this by, e.g. `vector(const allocator_type& a = allocator_type());` – Charles Salvia Apr 11 '12 at 11:08
  • @pmr: I don't necessarily agree they are evil. However I agree that 1. the constructor should be marked `explicit` if it can be called with a single argument and 2. when parameters abund the function is a pain to use... but neither is specifically linked to default parameters per se. – Matthieu M. Apr 11 '12 at 11:50
  • @CharlesSalvia Making objects default constructible is one of the nice use cases for default arguments. I put "evil" in quotes to indicate that there are use cases but that some care has to be taken and they shouldn't be the first option. – pmr Apr 11 '12 at 11:56
  • @pmr: "evil" is bad word choice.. maybe deceiving is a better word. –  Apr 11 '12 at 13:11
  • I edited for terminology. `int x` is a parameter; values which are supplied in a function call corresponding to that parameter are called *arguments*. – M.M Jul 21 '17 at 03:54
3

The assignments in declarations provide default values for optional parameters. Object() means a call to Object's default constructor.

The effect of the default parameters is as follows: you can invoke ListNode constructor with zero, one, or two parameters. If you specify two parameter expressions, they are passed as usual. If you specify only one expression, its value is passed as the first parameter, and the second one is defaulted to NULL. If you pass no parameters, the first parameter is defaulted to an instance of Object created with its default constructor, and the second one is defaulted to NULL.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

go to http://www.errorless-c.in/2013/10/operators-and-expressions.html for operators and expressions in c programming language