2
template <typename Type>
class Stack
{
private:
    int stack_size;
    int array_capacity;
    Type *array;

public:
    Stack( int = 10 ); //??
    ~Stack();
    bool empty() const;
    Type top() const;
    void push( const Type & );
    Type pop();
};

template <typename Type>
Stack<Type>::Stack( int n ) :
    stack_size( 0 ),
    array_capacity( std::max(0, n) ),
    array( new Type[array_capacity] )
{
    // Empty constructor
}

This is an implementation of a stack using a one ended array, however bits of the code is confusing me. I don't understand why it says int = 10.

Please explain, thanks :)

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
mr3mo
  • 135
  • 1
  • 10
  • 4
    Unless I am missing something, that is just default arguments: http://en.wikipedia.org/wiki/Default_argument – Nican Feb 09 '14 at 15:45

4 Answers4

12

It's an unnamed parameter with default value of 10.

You don't need to name parameters at the declaration of a function - all the compiler cares about are types and number of parameters.

At the definition, the name is required if you wish to use the parameter inside the body.

Nota bene: It's a declaration of default constructor, because it can be called without arguments.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • Never encountered it to be honest. What is it useful to if you cannot use it? – Marco A. Feb 09 '14 at 15:47
  • @DavidKernin You can use it if you give it a name in the definition of the function. – Joseph Mansfield Feb 09 '14 at 15:48
  • 1
    @DavidKernin [Tag dispatching](http://www.generic-programming.org/languages/cpp/techniques.php#tag_dispatching), for example. Not naming it avoids compiler warnings about unused parameters. Also, hiding of implementation details, reserving parameters for future use, etc... – jrok Feb 09 '14 at 15:48
  • 1
    @DavidKernin Backward compatibility as well. Sometimes an argument becomes unused and you don't want to trigger "argument unused" warnings. Removing the name prevents those. – pmr Feb 09 '14 at 15:50
  • Thank you for the basic answer. :) – mr3mo Feb 09 '14 at 16:21
  • Now I get it, but what's the point into assigning it a default value? You can't use it anyway! – Marco A. Feb 09 '14 at 16:34
  • @DavidKernin The declaration is seen everywhere, definition might not be. Even though the name of the parameter is not needed for the caller, the default value is if they want to use it. [Related](http://stackoverflow.com/questions/4989483/where-to-put-default-parameter-value-in-c). – jrok Feb 09 '14 at 16:46
  • uh, also a comment might have sufficed then.. but okay. Thanks – Marco A. Feb 09 '14 at 17:02
5

This gives a default value to the first argument of the constructor. This also makes the class default-constructible and implicitly convertible from int. This can have some surprising effects.

struct X {
  X(int = 10) {  }
};

void foo(X) {}

int main()
{
  X x1; // works
  X x2 = 23; // works
  foo(20); // works

  return 0;
}

This seems undesirable for a stack and you should add the explicit keyword to the constructor.

pmr
  • 58,701
  • 10
  • 113
  • 156
2

This is a default parameter. you can call the constructor with or without the argument. If you don't specify the argument, it's value will be 10.

elyashiv
  • 3,623
  • 2
  • 29
  • 52
1

Stack( int = 10 ); //??

10 is just the default value for your parameter, it will be assigned to your parameter if it is not provided during constructor call.

The fact that compiler allows you to not give names to parameters in definition, does not mean it is a good practice. Definitions are mostly used with additional comments about what given function/constructor is doing, also they relate to given parameters by names, so choosing unnamed parameters in definitions is actually bad practice.

Additionally you constructor will allow you to do silly stuff like:

Stack<int> dd = 0;

or:

void fun(const Stack<int>& d) {}
fun(0);    

to make it not possible change your constructor to explicit:

explicit Stack( int = 10 );

this will cause compiler to generate error for earlier statement:

error: conversion from 'int' to non-scalar type 'Stack' requested

marcinj
  • 48,511
  • 9
  • 79
  • 100