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

x
?
Solved

Omit optional parameter in function call

Posted on 2009-07-14
5
Medium Priority
?
787 Views
Last Modified: 2012-05-07
Hi,

You'd think this was easy to figure out and yet I can't find any information on the net regarding this.  

Here is a function in C++
void Vehicle(int p = 4, int f = 16, int m = 12)

when calling the function, how can you omit one of the parameters? You can omit the last by saying
Vehicle (2, 14);
But how do you omit the first or second?

In VB you'd simply say
Vehicle (,,2) but you can't seem to do that in C++

Thanks
0
Comment
Question by:Cyber-Storm
  • 2
  • 2
5 Comments
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 total points
ID: 24850569
You can't.
The optional parameters only go from right to left, it is not possible to omit one to the left and supply the ones firther to the right.
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 total points
ID: 24850600
ps.
You can use extra functions to supply the 'missing' parameters

eg.
void VehicleX(int p = 4, int m= 12) { Vehicle(p, 16, m) ; };
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 1200 total points
ID: 24850651
I think I understood the question and the answer is no, not in C++.
I'd like to remind you one thing that can be interesting for you:
 

/* vprintf example */ 
#include <stdio.h> 
#include <stdarg.h> 
 
void WriteFormatted (char * format, ...) 
{ 
    va_list args; 
    va_start(args, format); 
    vprintf(format, args); 
    va_end (args); 
} 
 
int main () 
{ 
    WriteFormatted("Call with %d variable argument.\n",1); 
    WriteFormatted ("Call with %d variable %s.\n",2,"arguments"); 
    return 0; 
}

Open in new window

0
 

Author Closing Comment

by:Cyber-Storm
ID: 31603368
Thanks very much guys.  Yes, the overloading is a definate option I need to bear in mind for more intricate functions.

As for the example app, that's very interesting, thanks very much.

I've split the points between you guys based on feedback, I hope it's ok?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24857277
Thanks. You welcome
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month6 days, 2 hours left to enroll

772 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