const

Can anybody please tell me the issue with the below is due to the const keyword or any other reason?
the following does not work:-
bool fun(const CString strName)
{
CUpdate param;//User defined calss
param.pObject = reinterpret_cast<CObject*>(strName);
--------
-----
}

but this works:-
bool fun(const CString strName)
{
CString strJobName = strName;
CUpdate param;//User defined calss
param.pObject = reinterpret_cast<CObject*>(strJobName);
--------
-----
}
reinterpret_cast can convert any non-const pointer to any other non_const pointer.

You cannot use this typecast form with objects so I am surpised that any of your examples work.
CString derives from CObject, so it is OK to cast them, but like weaknessforcats said, it is meant to be used with pointers.

The second appears to work because strJobName isn't const to begin with (but it doesn't because you are blithely casting a CString to a pointer).

However, the problem is because reinterpret_cast<> cannot cast away constness.

Since CString derives from CObject, you might as well just skip the reinterpret_cast<> and do it thus:
1
2
3
4
5
6
bool fn( const CString strName )
  {
  CUpdate param;
  param.pObject = const_cast <CString*> ( &strName );
  ...
  }


Hope this helps.
Last edited on
Hi,

Thanks weaknessforcats for the info,
Duoas: CString does not have a base class.

Hi Duoas,

Please do not mind but i have something to say for you,
I have observed this kind of wrong concept written by you for the second time now.
1st is the above mentioned CString
and
second is your reply for stack concept at, "The design and evolution of C++", by satm2008

My point is we should be careful while posting any answers. At least conceptually.

Wat all the forum guys say, am i correct?
So out of some 700 posts I've made a typo and a mistake.

I wish you high-and-mighty people would get off your pedestals and stop wasting bandwidth attacking people instead of responding to problems. I don't mind being corrected, but I do mind being abused by weenie-wannabes.

The problem is that you are misusing reinterpret_cast<> when you should be using const_cast<>. But, considering CString doesn't derive fro CObject, then what makes you think your code will work to begin with?

At least satm2008 has the good sense to give someone the benefit of error.

So congradulations, you've officially made my ignore list.
Last edited on
@KDevloper
everybody is careful posting answers. The windows class hierarchy is extremely complicated like everything else microsoft touch and it is very easy to make mistakes about what inherits from what etc. If it had been me answering the post, i would have assumed from your code that you had figured out that it did derive from CObject, it is a reasonable assuption unless you actually know, I suspect Duoas made the same assumption. Trying to rub an error in someones face, that was caused by assuming you knew what you were trying to do is not the way to make friends in here,

Following on from Duoas and bnbertha. Microsoft do an absolutely crap job of implementing anything even remotely close to a standard (that they didn't develop).

.NET C++ has alot of wonderful changes that most C++ developers (who use non-microsoft kit) will not know about. Therefore, in order for us to provide some help we have to make assumptions about Microsoft's decisions. It's not unreasonable to assume everything inherits from CObject (similar to Java, that Microsoft "borrowed" for C#/.NET).

FWIW, I would use std::string over CString.
Hi Duoas,

I have not used any abusing language here.If you do not mind being corrected, then why do you turn things in a bad way. You can ignore me or i can ignore you also , but we are here as a team to correct each other.i agree with your point also of misusing reinterpret_cast<> when i should be using const_cast<>, So i thank you for the suggestion.

While using MFC, we Should not assume that all classes are derived from CObject.

Any way dont take it in any other way..dear.. you and i and all of us here are high-and-mighty people(Actually wat does it mean?).
Last edited on
Topic archived. No new replies allowed.