|
From: Ioannis V. <iv...@em...> - 2003-09-05 22:54:26
|
> -----Original Message-----
> From: Ioannis Vranos [mailto:iv...@em...]
> Sent: Friday, September 05, 2003 6:20 PM
> To: Dev-C++ Mailing List (dev...@li...)
> Subject: reference question
>
>
> C++:
>
>
> Has anyone any idea why this compiles:
>
> int main()
> {
> char c;
>
> char * const &r=&c;
> }
>
>
>
> and this does not:
>
>
> int main()
> {
> char c;
>
> char * &r=&c;
> }
And the answer is that a temporary char * const (&c) is assigned to char *
const &r. That is a temporary pointer variable of the type char * const
(const means it cannot point in another char) is created, holding the
address of c.
An example code:
#include <iostream>
int main()
{
char c=0;
char * const &r=&c;
std::cout<<static_cast<void *>(r)<<std::endl;
std::cout<<static_cast<void *>(&c)<<std::endl;
}
But in general references can be used to point to specific pointers, using
templates only.
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|