Solved

19. Why we need constant method? What is the difference of defining constant method of usual method?

Posted on 2015-01-24
2
84 Views
Last Modified: 2015-02-11
19. Why we need constant method? What is the difference of defining constant method of usual method?
0
Comment
Question by:Nusrat Nuriyev
[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
2 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40568224
A constant method differes from all other methods by the fact that it is not allowed to make any changes to data members of the class it belongs to. Consider e.g. a method that basically prints out the data - it won't alter it. So that's a ideal candidate for making it 'const' and thus allow not only the compiler to make some optimizations but also make clear to others who might derive from that class that altering any data when overriding this method has to be avoided.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40568230
Ooops, there is a notable exception - any data declared as 'mutable' can be altered from a const method, e.g.

class MyClass {
public:
    void foo(int x) const {
        std::cout << "this is 'foo()' called with " << x << std::endl;

        value = x; // if 'value' was not mutable, the compiler would throw an error here
    }
protected:
    mutable int value;
};                         
                                          

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

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