1

in C++ classes, if I have a constant function (In my understanding constant functions are classes that cannot change anyting in the class, hope this is true) for example a function like this

int output(int value) const 

Am I allowed to call a function within this function remove that actually changes anything in the class?

For example lets say I have a variable called number in private.

my output function adds one to the variable number, but not directly, it calls a non constant function called addOne which adds one to the variable number. I notice that I am getting an error when a output function is calling addOne(I know that output function is const but it modifies a value, but it modifies it indirectly). I wonder why I am getting an error, what error compiler actually detects here?

Thank You, and sorry if my question is hard to understand.

Thank you all for answering.

5
  • 1
    Are you wondering why your function that promises not to change anything isn't allowed to change anything? Commented Apr 12, 2015 at 0:59
  • 1
    It is possible to indirectly modify an object through a const method, if you can find a non-const pointer to it from somewhere else. Commented Apr 12, 2015 at 1:06
  • @NeilKirk again a good point :) And this comes back to the old discussion of "bit-wise const" via "logical const". Commented Apr 12, 2015 at 1:11
  • @molbdnilo that is exactly what I was wondering about. I understand it sounds stupid, but I was curious because change was happening indirectly. Commented Apr 12, 2015 at 1:24
  • @user1335175 Along the lines of "I'm not allowed to have a cookie, but my brother is, so it should be OK if he takes one and gives it to me"? No, that's still not OK. Commented Apr 12, 2015 at 1:33

4 Answers 4

2

The answer is simple - no, you cannot. Every class method is a function that implicitly passes this as a parameter. When you declare a method as const, that means that it will actually pass const this to this function. It is both correct logically and syntactically not to call not const methods from const methods.

Sign up to request clarification or add additional context in comments.

3 Comments

@vsoftco, yeah sure. It is a good point to use mutable members when you want to cache some calculations that is done in const methods in order to get a better response time.
Actually I take it back, you cannot call non-const member functions from inside const methods, no matter what. I misread the question, upvoted your answer.
You can actually const_cast<Foo*>(this) and call a const method, but it only works ok on non-const objects. On const objects it leads to undefined behaviour.
2

const instances can only call const functions. Say:

class YourClass { ... int output(int value) const; int regularMethod(); // Non-const ... }; 

is your class with a const function. And the variables:

const YourClass obj1; YourClass obj2; 

Here obj1 can call output method while it cant call regularMethod. obj2 can call both.

3 Comments

This answer is too vague. What is a "regular" instance? Also, you can have a non-const instance accessed through a const reference.
obj2 can call the output function.
@NeilKirk: It was my bad sorry!
2

Every const function() is getting a const this pointer as a default parameter, so you cannot change a member variable of a class even if you try to call a non_cost function from a const function and try to modify a member variable.

So if you try to call a non const function from a const function, you will get compile error.

Comments

1

The answer is NO. The long answer is perhaps YES, provided you use an abomination like this, via const_cast-ing this in the const calling function (but you have to make sure you invoke your function on non-const objects, otherwise it leads to undefined behaviour):

#include <iostream> struct Foo { int x = 0; void f() { ++x; } void modify_x() const { const_cast<Foo*>(this)->f(); // call non-const member function } }; int main() { Foo foo; std::cout << foo.x << std::endl; foo.modify_x(); std::cout << foo.x << std::endl; } 

2 Comments

Thank You for the answer. Also thank you for additional clarification in regards to mutable, but at this point I never worked with mutable and have no idea what it is :).
@user1335175 actually I misread your question, and Anton's answer is the correct one, I clarified mine. mutable lets you modify non-const member variables via const methods. But it cannot allow calling non-const methods from inside const methods.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.