Solved

Mysterious default argument.

Posted on 2003-10-27
5
252 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

Independent Software Vendors: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

740 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