09-05-2005
Quote:
Originally Posted by arun.viswanath
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
Arun,
value of '3' is a constant hence it can only be assigned to a const reference. below example will help you to understand better.
likewise say if you have:
int a=3;
int &b = a; //this is perfectly legal
const int &c = a; //this is legal
while if you had
const int a=3;
int &b = a; //illegal because : cannot convert from 'const int' to 'int &'
const int &c = a; //this is legal
rishi
9 More Discussions You Might Find Interesting
1. Programming
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
2. Red Hat
Hello everey one,
here i am attempting to compile a c++ project .it's throughing the following errors.
my machine details are as follows:
Linux chmclozr0119 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux
errors:
=====
Generating... (0 Replies)
Discussion started by: mannam srinivas
0 Replies
3. Programming
Is it possible to have a non-const reference variable as an OPTIONAL/DEFAULT parameter to c++ function
ex
void read(string &data,int &type=0 /*or something*/) ;
so i will call
read(data);
//or
int type;
read(data,type);
printf("Type =%d",type);
I found one dirty workaround
... (2 Replies)
Discussion started by: johnbach
2 Replies
4. Programming
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. Programming
Is it permitted to type-cast a member variable passed in as a parameter in a member const method ..
I am doing something like :
in a
return-type method () const {
variable other = long(member variable)
}
this other assigned variable is not updating and I wonder if type-casting in such... (1 Reply)
Discussion started by: shriyer123
1 Replies
6. Programming
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
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
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
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
LEARN ABOUT DEBIAN
cmp_set_offset
COMPARE(3pub) C Programmer's Manual COMPARE(3pub)
NAME
cmp_set_offset, cmp_struct, cmp_char, cmp_short, cmp_int, cmp_long, cmp_float, cmp_double, cmp_long_double, cmp_schar, cmp_uchar,
cmp_ushort, cmp_uint, cmp_ulong, cmp_charptr, cmp_chararr - comparison functions for qsort, bsearch, and others
SYNOPSIS
#include <cmp.h>
void cmp_set_offset(size_t offset, int (*)(const void *, const void *));
int cmp_struct(const void *, const void *);
int cmp_char(const void *, const void *);
int cmp_short(const void *, const void *);
int cmp_int(const void *, const void *);
int cmp_long(const void *, const void *);
int cmp_float(const void *, const void *);
int cmp_double(const void *, const void *);
int cmp_long_double(const void *, const void *);
int cmp_schar(const void *, const void *);
int cmp_uchar(const void *, const void *);
int cmp_ushort(const void *, const void *);
int cmp_uint(const void *, const void *);
int cmp_ulong(const void *, const void *);
int cmp_charptr(const void *, const void *);
int cmp_chararr(const void *, const void *);
DESCRIPTION
The functions declared above, with the exception of cmp_set_offset, compare two array elements of the indicated type when given pointers to
them. The functions are designed to work with qsort(3), bsearch(3), and a number of other library functions which all need the same type
of comparison function. They all return negative if the value indicated by the first argument is less than the second, 0 if they are the
same, and positive otherwise.
cmp_struct compares elements of a structure. It needs to know the offset of the element, and a comparison function suitable for the type
of the element. These are set with cmp_set_offset, which should be called before qsort(3). The settings stay in effect until changed.
SEE ALSO
publib(3), qsort(3), bsearch(3), isort(3), lsearch(3), lfind(3)
AUTHOR
Lars Wirzenius (lars.wirzenius@helsinki.fi)
Publib C Programmer's Manual COMPARE(3pub)