?
Solved

MyMethod()const ..question

Posted on 1998-11-27
2
Medium Priority
?
215 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 240 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 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