Solved

Omit optional parameter in function call

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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