![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| get positive number n as argument script must calculate the factorial of its argument | I-1 | Shell Programming and Scripting | 3 | 04-28-2009 09:24 AM |
| Mac OS X 10.5.5 and later: Installing optional and bundled software | iBot | OS X Support RSS | 0 | 03-19-2009 10:50 PM |
| How to make parameters optional? | neeto | Shell Programming and Scripting | 2 | 05-23-2008 11:57 PM |
| diff b/w char const in C/C++ | sarwan | High Level Programming | 3 | 10-11-2005 02:14 AM |
| Reference to a const | arun.viswanath | High Level Programming | 1 | 09-05-2005 06:20 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Is it possible to have a non-const reference variable as an OPTIONAL/DEFAULT parameter to c++ function
Code:
ex void read(string &data,int &type=0 /*or something*/) ; Code:
read(data);
//or
int type;
read(data,type);
printf("Type =%d",type);
Code:
#include<iostream>
#include<string>
using namespace std;
int dummy;
void read(string &data,int &type=dummy)
{
data="TESTDATA";
type=99;
}
int main(void)
{
string val;
int type;
read(val,type);
cout<<"TYPE:"<<type<<"\n";
//or if i dont want the value of type
read(val);
}
Any suggestion? Last edited by johnbach; 09-17-2009 at 04:08 AM.. |
|
||||
|
google for C++ variadic template - stdarg
You may also want to look at this - C variadic function syntax: Variadic Example - The GNU C Library |
|
||||
|
Code:
void read(string &data,int &type=0 /*or something*/) ; Put a "const" in front of the "int" and it will compile. Why? Because it makes sense. A reference-to-const is read-only. There's nothing to be lost. So, in conclusion. The "workaround" isn't really that dirty, because it keeps upright meaning of passing by reference. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|