Solved

Mysterious default argument.

Posted on 2003-10-27
5
241 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 50 total points
ID: 9627246
zpivat,
  You have 5 open questions. It is a good practice to accept answers.
...Shu
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now