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
Solved

Mysterious default argument.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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