BTW, more about how these casts work at http://www.cplusplus.com/d
Main Topics
Browse All TopicsExisting code uses const in getDBPassword function to get password from an ini file. I need to decrypt that password value but because the existing code uses const the value never changes.
What is a (if any) work around to this without totally re-writing the new class? Can the existing below function getDBPassword or setDBPassword be modified to accommodate my problem? How?
This is the existing implementation that gets cleartext PW: setDBPassword(iniFile.getD
const _bstr_t & getDBPassword() const
{
return bstrDBPassword;
}
void setDBPassword(_bstr_t newDBPassword)
{
bstrDBPassword = newDBPassword;
}
My decrypt implementation: decrypt(newDBPassword, pKey);
decrypt(char *pCode, const char *pKey)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
BTW, more about how these casts work at http://www.cplusplus.com/d
Infinity08,
I need to change bstrDBPassword in the below function:
const _bstr_t & getDBPassword() const
{
return bstrDBPassword;
}
The existing code gets this value using this call: setDBPassword(iniFile.getD
I need to wrap it with: decrypt(char *pCode, const char *pKey) so that the bstrDBPassword value it gets from an ini file will decrypt. The decrypt function works fine, it was successfully implemented in two other C++ programs that do not use const _bstr_t. This particular one uses const _bstr_t causing me problems. My C++ is way rusty.
Thanks
-CZ
I still don't understand the problem.
iniFile.getDBPassword() returns a reference to const _bstr_t. You can do with that reference whatever you want (everything except modify the _bstr_t it's referring to) ... If you need a modifiable version of it, just copy it like evilrix suggested :
_bstr_t copy = iniFile.getDBPassword();
and you can use the copy for whatever purpose you need it.
How does the decrypt function fit in all this ? How does the setDBPassword function fit in all this ? Could you show us in code what you're trying to do, but doesn't work ?
Btw, A const_cast is really a last resort solution, as it can cause more problems than it solves. I'm pretty sure there's a better way of doing this, but we'll need a bit more information for that.
evilrix and Infinity08,
Thanks for your set of eyes and help. After much frustration and trying both of your solutions, twice, I concluded that I must have a config problem. My final implementation worked after correcting a dumb IDE oversight. As they say the problem was between the seat and the keyboard!
setDBPassword(_bstr_t(decr
Thanks again,
-CZ
Business Accounts
Answer for Membership
by: jkrPosted on 2009-10-20 at 13:07:46ID: 25617994
Why not using a 'const_cast()' (http://www.cppreference.c om/wiki/ke ywords/con st_cast), e.g.
Select allOpen in new window