The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-17-2009
johnbach johnbach is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 53
Question Optional/DEFAULT non-const reference argument c++ ?

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*/) ;
so i will call

Code:
read(data);
//or
int type;
read(data,type);
printf("Type =%d",type);
I found one dirty workaround

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..
  #2 (permalink)  
Old 09-17-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,759
google for C++ variadic template - stdarg

You may also want to look at this -
C variadic function syntax:
Variadic Example - The GNU C Library
  #3 (permalink)  
Old 09-17-2009
Gunther Gunther is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 37
Code:
void read(string &data,int &type=0 /*or something*/) ;
It might look cleaner and more concise, but remember that passing a variable by reference means to change its *actual* value. Since type, however, will be destroyed when read() ends, it wouldn't make too much sense to change its default value to something else. The change would be lost anyway.

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.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0