Solved

MyMethod()const ..question

Posted on 1998-11-27
2
211 Views
Last Modified: 2010-04-01
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()" ???
0
Comment
Question by:nil_dib
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 80 total points
ID: 1178746
Assuming that m_nX is a mebmer of 'B', this statement is not legal, as functions that are declared 'const' may not alter _any_ member variable (the compiler will also complain ;-).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178747
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question