473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Avoiding const_cast ?

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
Jul 22 '05 #1
3 2103

"Fred" <Fr**@somewhere.abc> wrote in message
news:cl**********@sparta.btinternet.com...
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
Jul 22 '05 #2

"Fred" <Fr**@somewhere.abc> wrote in message
news:cl**********@sparta.btinternet.com...
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
Jul 22 '05 #3
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: drowned | last post by:
all right, check it out... I've got a practice exercise from "thinking in c++" whose answer isn't covered in the annotated solutions guide, so I'm trying to handle it, but I don't understand what...
7
by: R. Anbeeswaran | last post by:
Hi All, void main() { const int i = 20; int *p = const_cast<int*>(&i); *p = 40; cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n"; }
4
by: S.Senthilvel | last post by:
hi, I am a little confused about the const_cast.I've thought it this way till now. //Proper cast int i = 10; const int* pci = &i; int *pi = const_cast<int*>(pci);
18
by: johny smith | last post by:
Can someone please give me some good reference site/information for understanding when and why I would use 1.) const_cast Don't know why you would do this? 2.) reinterpret_cast. I have used...
6
by: AlesD | last post by:
Hello, I can't figure out how to build assignment operator for class which contains "type* const value". --- example --- class parent_t; class child_t {
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
6
by: Alexander Stippler | last post by:
Hello, I wonder if and in what cases casting const away by a const_cast can be dangerous. In one place the only way I can imagine to realize a pretty nice feature is casting away const. Now I...
4
by: George2 | last post by:
Hello everyone, In MSDN sample for const_cast, http://msdn2.microsoft.com/en-us/library/bz6at95h(VS.80).aspx There is a statement like this, --------------------
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.