Results 1 to 5 of 5

Thread: Default arguments

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    8

    Default arguments

    My problem is that I want to call a fuction with an array as its parameter and I want it to display entiore contents of that array if I do not send any other parameter but if I send an index as another parameter, it should display only the contents of that index. This can be done through Default Parameter as O but it is not working correctly. How can I successfully do this?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    void func(int *ptr, int sz, int idx = -1) {
        if(idx != -1 && idx >= 0 && idx < sz) {
            cout << ptr[idx] << endl;
        } else {
            for(int i = 0; i < sz; ++i) {
                cout << ptr[i] << endl;
            }
        }
    }
    Is that sort of what you mean?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by parksie
    idx != -1 && idx >= 0
    Mr. Parks! You should take some logic lessons soon!
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I think I'll take them now...oops

    I'd started with != -1 then decided to put a check for anything out of bounds and forgot the -1 was still there...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    My problem is that I want to call a fuction with an array as its parameter and I want it to display entiore contents of that array if I do not send any other parameter but if I send an index as another parameter, it should display only the contents of that index. This can be done through Default Parameter as O but it is not working correctly. How can I successfully do this?
    Why do you want to do this? VB habit? Get rid of immediately. For two different operations (by two different calls) have two different functions, ex displayall and displayone
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width