|
-
Jan 20th, 2002, 05:04 AM
#1
Thread Starter
New Member
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?
-
Jan 20th, 2002, 06:08 AM
#2
Monday Morning Lunatic
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
-
Jan 20th, 2002, 02:40 PM
#3
transcendental analytic
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.
-
Jan 20th, 2002, 04:29 PM
#4
Monday Morning Lunatic
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
-
Jan 20th, 2002, 04:37 PM
#5
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|