Newbie Question: Initializing a reference to the address of a const object

Newbie Question: Initializing a reference to the address of a const object

Post by Cus » Wed, 30 Jul 2003 07:23:29



Hi,

I had a question about initializing a reference to the address of a const object.

I did something like this:

        const int cint = 144; //const object
        const int *cptr = &cint;// Address of the const object

        const int * &ref = cptr;// reference to the addr of const obj.
This is working fine.

However if I try something like this :
        const int cint = 144; //const object
        const int * &ref = &cint;// reference to the addr of const obj.
The compiler is giving an error.

I think I am doing the same thing in both the cases, or am I going wrong anywhere.

Cheers!

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Newbie Question: Initializing a reference to the address of a const object

Post by Ulrich Eckhard » Wed, 30 Jul 2003 19:12:25


 > I did something like this:
 >
 >         const int cint = 144; //const object
 >         const int *cptr = &cint;// Address of the const object
 >
 >         const int * &ref = cptr;// reference to the addr of const obj.
 > This is working fine.
 >
 > However if I try something like this :
 >         const int cint = 144; //const object
 >         const int * &ref = &cint;// reference to the addr of const obj.
 > The compiler is giving an error.
 >
 > I think I am doing the same thing in both the cases, or am I going wrong
 > anywhere.

The problem is that C++ doesn't allow you to bind a 'reference to a
non-const object' to a temporary. In this case, '&cint' creates a
temporary object of type 'const int*const'. As soon as this line is
finished, this object ceases to exist and you would have a reference to a
non-existing object.

In case you wondered why I call a 'const int * &' a 'reference to a
non-const object', the reason is that I can well modify the pointer, but
not the object it points to, the pointer _is_ modifyable.

const int * const & ref = &cint; // should work !

Two final words:
1. I simplified the lifetime of temporary, but it doesn't matter in this
case.
2. I'd prefer 'int const*' over 'const int*' because writing const (or
volatile) on the right is the only thing that always works. See the FAQ
for a full explanation. However, this is an almost religious issue.

Uli

--
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Newbie Question: Initializing a reference to the address of a const object

Post by llewell » Wed, 30 Jul 2003 19:16:44


 > Hi,
 >
 > I had a question about initializing a reference to the address of a
 > const object.
 >
 > I did something like this:
 >
 >         const int cint = 144; //const object
 >         const int *cptr = &cint;// Address of the const object
 >
 >         const int * &ref = cptr;// reference to the addr of const obj.
 > This is working fine.
 >
 > However if I try something like this :
 >         const int cint = 144; //const object
 >         const int * &ref = &cint;// reference to the addr of const
[snip]

The expression '&cint' creates a temporary. You cannot bind a
     temporary to reference to non const. However you may bind
     temporary to a reference to const:

           const int * const & ref= &cint

           which is the same as:

           int const * const & ref= &cint

     Either will solve your problem.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Newbie Question: Initializing a reference to the address of a const object

Post by John Potte » Wed, 30 Jul 2003 19:17:36



 > I did something like this:

 >         const int cint = 144; //const object
 >         const int *cptr = &cint;// Address of the const object

 >         const int * &ref = cptr;// reference to the addr of const obj.

No.  It is a reference to a pointer to a const int.  The pointer is an
lvalue.

 > However if I try something like this :
 >         const int cint = 144; //const object
 >         const int * &ref = &cint;// reference to the addr of const obj.
 > The compiler is giving an error.

Yes.  &cint is an rvalue and may not be bound to a non-const reference.

    int const* const& ref = &cint;

Now you have a reference to a const pointer which may be bound to an
rvalue by consturcting a copy of the rvalue and extending its lifetime
to that of the reference.

John

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. Initialize a reference to the address of a const object

One of the textbooks that I am using while learning C++ is Stanley
Lippman's The C++ Primer. On page 108 of this book he give the
following code as an example as to how to initialize a reference to
the address of a const object.

        const int ival = 13;
              int * const  &ref = &ival;

The problem is that this generates errors in both Visual C++ and g++.
What is wrong with this example, if anything?


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. NEW NEW NEW: Gimme A Clue by Ninjaforce

3. temporary object used to initialize non-const. reference

4. cw2dmk 1.3: read any TRS-80 floppy with a Catweasel

5. const reference, not reference to const

6. Mouse/Modem

7. Const references (not references to const) ??

8. Initializing a temporary with a non-const reference

9. Initializing a const object

10. initializing class data member that contains object references

11. Returning non-const reference from a const function

12. Newbie Question: Initialising Iterator