Link to home
Start Free TrialLog in
Avatar of nil_dib
nil_dib

asked on

MyMethod()const ..question

suppose I have two classes with a const method in the
derived class:
class A
{
 int m_nX;
};
class B : public A
{
 int m_nY;
 int GetValue() const
 { return m_nY; }
};
in this case everthing is allright.
the question: is it legal/proper to do something like
class B : public A
{
 int m_nY;
 int GetValue() const
 {
  m_nX = 4;
  return m_nY;
 }
};
concerning the const method "GetValue()" ???
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

Note that on compilers that support it, the m_nX member could be declared mutable.  mutable members may be changed from const functions.  However, indiscriminate use of "mutable" discouraged

Note also that m_nX is a private member of the base class.  Thus the error you will get is that m_nX is undefined, not that it is constant.  If it were made public or protected, then you would get the error that it is constant.