Solved

Omit optional parameter in function call

Posted on 2009-07-14
5
735 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 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 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 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 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 300 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

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

18 Experts available now in Live!

Get 1:1 Help Now