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

Omit optional parameter in function call

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with coding for USB Relay card 11 539
Error when running console app 9 142
.Net Errors Service Crashing 5 130
vb.net - How to check if current user is an administrator? 6 55
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

789 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