default parameter

This is a discussion on default parameter within the C++ Programming forums, part of the General Programming Boards category; Code: class foo { public: foo(); bar(char m[4] = m_matrix); private: char m_matrix[4]; }; Is it possible to set a ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    default parameter

    Code:
    class foo
    {
      public:
      foo();
      bar(char m[4] = m_matrix);
      private:
      char m_matrix[4];
    };
    Is it possible to set a member variable (non-static) as a default parameter like Im trying to do in the bar(..) method above ? If so, how ? Appreciate any help.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Why don't you try writing that out and compiling it, then see what happens?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Not unless it is a static data member.

    You could always use = NULL and then check for null in your function, if you can't use static.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Obviously my code doesnt work otherwise I wouldnt have posted it here. The above was just an example to illustrate the problem.

    Appreciate any help you can give me.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Cat
    Not unless it is a static data member.

    You could always use = NULL and then check for null in your function, if you can't use static.
    Thanks

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    As something of an aside, passing NULL is the typical thing to do when you want to leave out an optional pointer parameter, so I think the =NULL is a better choice anyway.

    In your code, you just have the first line of the function be:

    if (!m) m = m_matrix;

    which sets m to point at the beginning of the array if it was NULL.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Yeah, I think that sounds like a smart way to solve my problem.

    Btw, a bit of novice question but is null defined as 0 ?

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Not exacty. But setting a pointer = 0 is the same as = NULL.
    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.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Nothing can reside at the 0 memory address, so 0 is used in C++ to represent the null pointer (in context). The actual bit pattern for the null pointer may not be a string of 0s, however (though what it is exactly is of no consequence).

    There's usually something to the effect of 'const int NULL = 0' or '#define NULL 0' somewhere so that they come out equivalent.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Default parameter
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2007, 11:59 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 04:21 PM

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