Assignment operator in a constructor argument

This is a discussion on Assignment operator in a constructor argument within the C++ Programming forums, part of the General Programming Boards category; Hi all - this is my first post to these forums, and I'm a novice programmer. I've been working through ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Question Assignment operator in a constructor argument

    Hi all - this is my first post to these forums, and I'm a novice programmer.

    I've been working through the book Teach Yourself C++ in 21 days. In one example, the following class declaration appears as a private member of a larger class (called List):

    Code:
    class ListCell
    {
    public:
       ListCell( int value, ListCell *cell = 0):val(value), next(cell) {} //why the assignment operator?
       int val;
       ListCell * next;
    }
    I understand that this ListCell class is used to create the cells of a linked list of integers. What confuses me is the second argument of the constructor. What does the assignment operator (=) in the second argument do? I didn't know the asignment operator had any place in argument lists.

    This is probably some basic syntax issue that I missed along the way. Any help?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    It's a default argument. It means that you can call that function with either two or one argument. if you call it with one argument, int value will be used and the second argument will default to 0.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    Thanks Mario!

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    It specifies a default argument.

    So, for example, you can do these:

    ListCell list1(10); // here cell defaults to zero (NULL)
    ListCell list2(5, &list1); // here we give an explicit value for cell so the 0 is ignored.

    The only caveat is that if you provide a default value for, say, argument x, you need to provide a default value for every argument after x as well.

    And if you have a function with multiple arguments which have defaults, and you want to specify a particular (nondefault) value for argument y, you need to specify a value for every argument before y. That is, if you had a function that had two arguments, each which had a default, and you want to set the second argument to a non-default value, you can't skip the first argument.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    Thanks Cat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 03:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21