473,698 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2129

"Fred" <Fr**@somewhere .abc> wrote in message
news:cl******** **@sparta.btint ernet.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.btint ernet.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 = lengthyCalculat ion();
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
5387
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 I'm doing wrong. Here is the exercise: 27. Create a const array of double and a volatile array of double. Index through each array and use const_cast to cast each element to non-const and non-volatile, respectively, and assign a value to each...
7
4302
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
2798
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
4840
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 this a little when I converted a primitive to a class, but would like to see other applications.
6
1627
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
2082
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 the constant variable and if I changed the value through the pointer, the constant variable should have been modified. Well, that's what I thought until I wrote the following and run it. #include <iostream> using std::cout; using std::endl;
6
2143
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
1932
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 wonder if this is dangerous. I have something like that: const Object & Object::dummy(const AType &a) const {
4
1818
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
8598
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8856
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.