A
amparikh
I know this is something fundamental and I ought to have known it, but
somehow this seems to be confusing me a lot.
Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.
Taking that into consideration, how does one get a constant reference
to a pointer.
class A
{};
1> A* foo(A* & ptr){ } // non-const reference to a pointer
2> A* foo( const A* & ptr) { } //reference to a pointer to const A
3> A* foo(A* const & ptr) { } // reference to a constant pointer to A
Now when I make a call like
foo( new A()); or
foo( &A());
The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.
It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).
Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...but it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.
But then how does one realy have a constant reference to a pointer in
this case ? const usually binds to the left(unless there is nothing to
the left, so then it binds to the right)...so in that case it would be
the same as declaration no 3.
which would mean that in reality, there is nothing as a const reference
to a pointer.
Thanks.
somehow this seems to be confusing me a lot.
Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.
Taking that into consideration, how does one get a constant reference
to a pointer.
class A
{};
1> A* foo(A* & ptr){ } // non-const reference to a pointer
2> A* foo( const A* & ptr) { } //reference to a pointer to const A
3> A* foo(A* const & ptr) { } // reference to a constant pointer to A
Now when I make a call like
foo( new A()); or
foo( &A());
The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.
It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).
Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...but it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.
But then how does one realy have a constant reference to a pointer in
this case ? const usually binds to the left(unless there is nothing to
the left, so then it binds to the right)...so in that case it would be
the same as declaration no 3.
which would mean that in reality, there is nothing as a const reference
to a pointer.
Thanks.