473,405 Members | 2,334 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,405 software developers and data experts.

Use const_cast to modify member variable?

How can I modify member variable inside class if member function has
const like mem_Func(void) const. Please do not offer the keyword --
mutable. I want to know if keyword -- const_cast can be done.

If mutable is only the solution, I will follow not to use const_cast.
Jul 1 '08 #1
1 2308

"Immortal Nephi" <Im************@satx.rr.coma écrit dans le message de
news: 1d**********************************...oglegroups.com...
How can I modify member variable inside class if member function has
const like mem_Func(void) const. Please do not offer the keyword --
mutable. I want to know if keyword -- const_cast can be done.

If mutable is only the solution, I will follow not to use const_cast.
Ok it is Evil but you can do it.

struct EvilClass
{
void EvilFunct() const;
int _a;
};

void EvilClass::EvilFunct() const
{
int& tmp = const_cast<int&>(_a);
tmp = 5; // now _a == 5
}
You can also define a template function if you have many variables like
that.

struct EvilClass
{
void EvilFunct() const;

template<typename T>
void EvilSet(T const& val, T const& NewVal) const;

int _a;
};

void EvilClass::EvilFunct() const
{
EvilSet(_a, 5); // much cleaner but maybe also much evil
}

template<typename T>
void EvilClass::EvilSet(T const& val, T const& NewVal) const
{
T& tmp = const_cast<T&>(val);
tmp = NewVal;
}

------------------------

Eric Pruneau
Jul 1 '08 #2

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

Similar topics

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: 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...
5
by: Tom Smith | last post by:
I hardly dare ask this given the furore in another thread over strings and const... My problem is this. I am assured that casting away the constness of the return value of std::string::c_str() is...
11
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
2
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.