Solved

Mysterious default argument.

Posted on 2003-10-27
5
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

738 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