assignment operator within function parameter C++

后端 未结 3 700
醉梦人生
醉梦人生 2020-12-09 19:44

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

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


        
相关标签:
3条回答
  • 2020-12-09 20: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

    0 讨论(0)
  • 2020-12-09 20:26

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

    0 讨论(0)
  • 2020-12-09 20:28

    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.

    0 讨论(0)
提交回复
热议问题