Avoiding const_cast ?

F

Fred

Hello

As mentioned in another post, I'm in the process of extending an existing
class.

One of the things I'm trying to achieve is an improvement in performance
through the use of some image caching. This means that the class itself can
serve up a scaled version of itself and use some basic caching to avoid
having to perform the scaling when the same scale value is used in
successive calls.

However there are many occasion in the code where only a const version of
the object is used - the intent being that the basic data defining the
object should not be changed. In these cases it should still be ok to change
any derived data - e.g. a cached scaled image. My question is: what is the
correct/best way of handling this.

At present I'm doing a const_cast but I always feel guilty doing this since
I feel like I'm undoing the good work of using consts in the first place.
Surely there must be a better way?

Thanks Fred
 
C

Catalin Pitis

Fred said:
Hello

As mentioned in another post, I'm in the process of extending an existing
class.

One of the things I'm trying to achieve is an improvement in performance
through the use of some image caching. This means that the class itself
can
serve up a scaled version of itself and use some basic caching to avoid
having to perform the scaling when the same scale value is used in
successive calls.

However there are many occasion in the code where only a const version of
the object is used - the intent being that the basic data defining the
object should not be changed. In these cases it should still be ok to
change
any derived data - e.g. a cached scaled image. My question is: what is the
correct/best way of handling this.

At present I'm doing a const_cast but I always feel guilty doing this
since
I feel like I'm undoing the good work of using consts in the first place.
Surely there must be a better way?
When the usage const_cast is necessary, first thing you have to do is to
look at the design. It might be not the best one.

However, you may think of using mutable keyword.

Catalin
 
J

John Harrison

Fred said:
Hello

As mentioned in another post, I'm in the process of extending an existing
class.

One of the things I'm trying to achieve is an improvement in performance
through the use of some image caching. This means that the class itself can
serve up a scaled version of itself and use some basic caching to avoid
having to perform the scaling when the same scale value is used in
successive calls.

However there are many occasion in the code where only a const version of
the object is used - the intent being that the basic data defining the
object should not be changed. In these cases it should still be ok to change
any derived data - e.g. a cached scaled image. My question is: what is the
correct/best way of handling this.

At present I'm doing a const_cast but I always feel guilty doing this since
I feel like I'm undoing the good work of using consts in the first place.
Surely there must be a better way?

The mutable keyword is designed for situations like this, e.g.

class X
{
mutable int cache;
int getValue() const
{
if (cache != 0)
cache = lengthyCalculation();
return cache;
}
};

john
 
F

Fred

Many thanks ....

I'd never come across the mutable keyword before - but it's just what I
needed.
I have now removed my const_cast and feel much better for it ;-)

Thanks again
Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top