Sponsored Content
Top Forums Programming Optional non-const reference argument c++ ? Post 302354181 by Gunther on Thursday 17th of September 2009 10:40:18 AM
Old 09-17-2009
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.
 

10 More Discussions You Might Find Interesting

1. Programming

Reference to a const

Can any one explain how the statement '2' in the following statements is a legal one. int & ref = 3; // Illegal statement - Compiler error. const int& ref=3 ; // Compile and executes properly. Thanks in Advance, Arun (1 Reply)
Discussion started by: arun.viswanath
1 Replies

2. Programming

diff b/w char const in C/C++

hi, what is the difference b/w char in C and C++.and give me the examples. Thanks... sarwan (3 Replies)
Discussion started by: sarwan
3 Replies

3. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

4. Programming

Questions about const

Hi all, I have some questions about functions. In a code I have (a .hpp file) there is this line which says: const Class_1_Name* Class_2_Name::MethodName(int ipart) const {return (ClassName*)_ref_to_method.At(ipart);} My questions are: What is the meaning of the two constants,... (2 Replies)
Discussion started by: eager2no
2 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Programming

Why this const variable is not changing even after applying const_cast?

Hi In the following code, why the variable 'i' still retains the value 10 instead of 11? Why it is not possible to alter this variable's value? int main() { const int i = 10; *(const_cast<int*>(&i)) = 11; cout << i << endl; // Ans: 10 } (6 Replies)
Discussion started by: royalibrahim
6 Replies

7. Programming

Using const char*

I am writing some code in C++ to print a message using fprintf Here is an example void pr_desc( FILE* stream, int shift, const char* desc) { const char* format="%*s\e; fprintf(stream,format,shift,"",desc); } I call it using const char* desc; ... (4 Replies)
Discussion started by: kristinu
4 Replies

8. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

9. Programming

Double to const char conversion

Dear all, I am using C and ROOT for programming. And I need to incorporate following in my code. char *fps=NULL; int dec=0,sign=0; float mean = h1->GetMean(1); //0.001298 fps= fcvt(mean,6 , &dec, &sign); I need to provide this mean as const char to some other function to get... (8 Replies)
Discussion started by: emily
8 Replies

10. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies
scsi_ext_sense_fields(9F)				   Kernel Functions for Drivers 				 scsi_ext_sense_fields(9F)

NAME
scsi_ext_sense_fields, scsi_sense_info_uint64, scsi_sense_cmdspecific_uint64 - retrieve fields from SCSI sense data SYNOPSIS
#include <sys/scsi/scsi.h> void scsi_ext_sense_fields(uint8_t *sense_buffer, int *sense_buf_len, uint8_t **information, uint8_t **cmd_spec_info, uint8_t **fru_code, uint8_t **sk_specific, uint8_t **stream_flags); boolean_t scsi_sense_info_uint64(uint8_t *sense_buffer, int sense_buf_len, uint64_t *information); boolean_t scsi_sense_cmdspecific_uint64(uint8_t *sense_buffer, int sense_buf_len, uint64_t *cmd_spec_info); INTERFACE LEVEL
Solaris DDI specific (Solaris DDI). PARAMETERS
sense_buffer Pointer to a buffer containing SCSI sense data. The sense data is expected in wire format starting at the response code. It can be in either a fixed or descriptor format. information For scsi_ext_sense_fields(), this is a call-by-reference pointer to uint8_t. A pointer to the INFORMATION field in the sense data may be returned in this parameter. The scsi_sense_info_uint64() function requires a pointer to uint64_t. The data in the information field is returned as a 64 bit integer. If the sense data information field is 32 bits, fixed format, the most significant 32-bits are 0. cmd_spec_info For scsi_ext_sense_fields(), this is a call-by-reference pointer to uint8_t. A pointer to the COMMAND_SPECIFIC INFORMATION field in the sense data can be returned in this parameter. The scsi_sense_cmdspecific_uint64() function requires a pointer to uint64_t. The data in the command specific information field is returned as a 64 bit integer. If the sense data command specific information field is 32 bits, fixed format, the most significant 32-bits are 0. fru_code Call-by-reference pointer to uint8_t. A pointer to the FIELD REPLACEABLE UNIT CODE field in the sense data can be returned in this parameter. sk_specific Call-by-reference pointer to uint8_t. A pointer to the SENSE KEY SPECIFIC field in the sense data can be returned in this parameter. stream_flags Call-by-reference pointer to uint8_t. A pointer to the byte containing the ILI, EOM, and FILEMARK flags can be returned in this parameter. DESCRIPTION
The scsi_ext_sense_fields() function can be used to retrieve any of the extended sense data fields from a sense data buffer, regardless of whether the sense data is in fixed format or in descriptor format. The information, cmd_spec_info, fru_code, sk_specific, and stream_specific parameters are all call-by-reference output parameters. Each parameter corresponds to one or more of the extended sense data fields. Any of these parameters can be passed as NULL if the caller is not interested in the associated data. If the requested data is present and valid for each output parameter, the pointer passed in is set to point to the relevant location in the sense buffer. If the data is not present or invalid, the pointer is set to NULL. For example, a caller that requests a pointer to the information field would get NULL when an information descriptor is not present for descriptor format sense data or when the valid bit is not set for fixed format sense data. The information and command specific information fields can be 4 bytes or 8 bytes in length, depending on whether the sense data is in fixed or descriptor format respectively. Drivers can use scsi_validate_sense(9F) to determine the sense data format and, by extension, the length of the information and command specific information fields. A driver can determine whether integer data is included in the information or command specific information fields based on the asc and ascq sense keys, such as the LBA of a failed disk request. The scsi_sense_info_uint64() function retrieves the contents of the information field as a 64 bit integer and the scsi_sense_cmdspecific_uint64() retrieves the command specific information field as a 64 bit integer. Drivers should use scsi_validate_sense(9F) to ensure that the sense buffer contains valid sense data. RETURN VALUES
The scsi_sense_info_uint64() function returns TRUE if the information field is present and valid. Otherwise it returns FALSE. The scsi_sense_cmdspecific_uint64() function returns TRUE if the command specific information field is present and valid. Otherwise it returns FALSE. CONTEXT
The scsi_ext_sense_fields(), scsi_sense_info_uint64() and scsi_sense_cmdspecific_uint64()() functions can be called from user or interrupt context. SEE ALSO
scsi_find_sense_descr(9F), scsi_sense_asc(9F), scsi_sense_ascq(9F), scsi_sense_key(9F), scsi_validate_sense(9F) SunOS 5.11 29 Jun 2006 scsi_ext_sense_fields(9F)
All times are GMT -4. The time now is 07:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy