M
Michael
This is the sample program:
#include<cstdio>
int main()
{
int*const a=new int;
const int*const&b=a;
printf("%p %p\n",&a,&b);
delete a;
return 0;
}
When running, it produces:
0x7fff1dc49fc8 0x7fff1dc49fb8
That means the memory locations of a and b are different i.e. a and b is
different object! I want to make something that *a is modifiable but *b is
not (to be used inside a class) but the following code generates a
compile-time error:
#include<cstdio>
int main()
{
int*a=new int;
const int*&b=a;
printf("%p %p\n",&a,&b);
delete a;
return 0;
}
test.cpp:6: error: invalid initialization of reference of type ‘const int*&’
from expression of type ‘int*’
The following code runs perfect:
#include<cstdio>
int main()
{
int a=new int;
const int&b=a;
printf("%p %p\n",&a,&b);
return 0;
}
What is the problem in the first code (I am using g++ 4.2.4)?
#include<cstdio>
int main()
{
int*const a=new int;
const int*const&b=a;
printf("%p %p\n",&a,&b);
delete a;
return 0;
}
When running, it produces:
0x7fff1dc49fc8 0x7fff1dc49fb8
That means the memory locations of a and b are different i.e. a and b is
different object! I want to make something that *a is modifiable but *b is
not (to be used inside a class) but the following code generates a
compile-time error:
#include<cstdio>
int main()
{
int*a=new int;
const int*&b=a;
printf("%p %p\n",&a,&b);
delete a;
return 0;
}
test.cpp:6: error: invalid initialization of reference of type ‘const int*&’
from expression of type ‘int*’
The following code runs perfect:
#include<cstdio>
int main()
{
int a=new int;
const int&b=a;
printf("%p %p\n",&a,&b);
return 0;
}
What is the problem in the first code (I am using g++ 4.2.4)?