Boost logo

Boost Users :

From: Gustavo Guerra (gustavobt_at_[hidden])
Date: 2002-01-17 12:03:57


----- Original Message -----
From: "Jonathan Brownell" <alphaomega_at_[hidden]>
> Essentially what I need is a const data member that is protected as a
> const except during House::operator =(const House&), at which point the
> entire class is replaced. Am I just out of luck? If so, this makes const
> class data members pretty useless in many cases, because similar
> situations to this have arisen quite a few times for me.
>

You could use Johan Ericsson idea:

>House& House::operator =(const House& source)
>{
>House temp(source);
>Swap(temp);
>return *this;
>}

and then use const_cast in swap:

void swap(const House& h)
{
  int temp = h.m_rooms;
  override_const_assign(h.m_rooms, m_rooms); // h.m_rooms = m.rooms
  override_const_assign(m_rooms, temp); // m_rooms = temp;
}

template<class T>
void override_const_assign(const T& lhs, const T& rhs)
{
  lhs2 = const_cast<T&>(lhs);
  lhs2 = rhs;
}

It's ugly, but it works.

HTH
Gustavo Guerra


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net