Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Omit optional parameter in function call

Posted on 2009-07-14
5
Medium Priority
?
782 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
[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
  • 2
  • 2
5 Comments
 
LVL 44

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 44

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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