Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mysterious default argument.

Posted on 2003-10-27
5
Medium Priority
?
266 Views
Last Modified: 2010-04-02
Hello,

I'm a beginner in C++, so even default argument gives me troubles! Heh!

Basically I have this little test program:

#include <iostream.h>

class defarg
{
    public:              
        char d;
        int i;
        float f;
   
        defarg(int iArg, char dArg = 'p', float fArg = 5.0)
             {
                 i = iArg;
                 d = dArg;
                 f = fArg;
             }          

};

int main()
{
    defarg test(5, 9.8);
    cout << test.i << " " << test.d << " " << test.f;
}

Now, before we begin, we all know that a default argument gets used only when we don't supply our own argument right? In other words, if we supply our own argument, then the default argument is disregarded, and if we don't supply, then the default argument gets used. Am I right here or what?

Now, my problem with the above program is, I want to supply my own i, use the default d (which is 'p'), and supply my own f.

In the main method I gave my own i (5), supplied nothing for d, and gave my own f (9.8), and so I expect the program to output 5 p 9.8.
However, that's not the case. It compiles fine, but its output is either 5 [blank] 9.8 or sometimes 5 [a weird looking character] 9.8.

Can anyone help me get the output I want? Or is it impossible?

Thanks.
   
0
Comment
Question by:zpivat
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9627098
defarg test(5, 9.8);

Result of this call is:

iArg = 5
dArg = (char) 9.8;   // 9.8 casted to char = 9
fArg = 5.0;              // default

Weird looking character in output is dArg (9).

It's impossible in C++ to skip parameter in the middle of the list.
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9627111
zpivat,
  You can only skip the last "n" arguments: You cannot skip an argument in between.
  So, if you have a function

int myfunc(int a, char b='1', char c='2', int d = 3);

then you can only call it in one of the following ways only:
myfunc(1)
myfunc(1, 'a')// -> b = 'a', others default
myfunc(1, 'a', 'b') // -> b = 'a', c = 'b', others default
myfunc(1, 'a', 'b', 0)// -> b = 'a', c = 'b', no default

Does that clarify?

And to get your output, you can change the function to
 defarg(int iArg, float fArg = 5.0, char dArg = 'p')

...Snehanshu
0
 

Author Comment

by:zpivat
ID: 9627195
Hi AlexFM and Snehanshu,

I'm clear now. So I was right; it's impossible afterall to skip the middle argument(s)!

Thank you!
0
 

Expert Comment

by:an_men
ID: 9627217
When using default arguments, only the trailing arguments may be defaulted.
That is, you cannot have a default argument followed by a nondefault argument.
Also once you start using default arguments, all the remaining arguments must be defaulted.
0
 
LVL 5

Accepted Solution

by:
snehanshu earned 200 total points
ID: 9627246
zpivat,
  You have 5 open questions. It is a good practice to accept answers.
...Shu
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question