Using const

Hi

Hopefully someone will be able to guide me in the usage of const.

I have a base class Parent, which has a pure virtual class function to be defined in the concrete child class Child. The class function Function() is const and takes two arguments, 1) a const array by reference: Array, and 2) a non-const vector by reference Vector. This is given as part of the established code structure.

My job is to write a child class of Parent to perform a certain task, so part of this is to define Function(). In Function() I want to process the values of Array to see if it meets a certain criteria. So in order to do so, I introduced a working variable say, bool Indicator to record when the ith element of Array meets the criteria.

The first problem is that because Function() is const, I can't change the value of Indicator within Function(). So then I moved the processing code to a non-const function ProcessingFn().

But when I call ProcessingFn() within Function() to do the processing, I get the error on ProcessingFn():

The object has type qualifiers that are not compatible with the member function.


So I thought maybe this is an issue with trying to pass the const Array into ProcessingFn() (which is a non-const function). However when I make a copy of Array in Function() (literally creating another array and looping through all values), and using the copy as the argument in ProcessingFn(), the error remains.

How do you call a non-const function within a const function? (If that is the right question to ask).

As always, any and all help much appreciated.
pzling wrote:
How do you call a non-const function within a const function?

You can't call a the object's non-const member functions from inside a const member function.

If you want Function() to modify member variables, why is it const to begin with?
Last edited on
Ah ok thanks, looks like I have to rethink how this is going to get done!
Topic archived. No new replies allowed.