ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
it seems that under some rare circumstances this cast returns a 0x0?? It has happened twice now in the past couple of weeks. I need the cast because of the const that was declared in the base class on the pure virtual method dosomething(). My question is why is this cast returning 0x0?? Aren't the old style pointer casts supposed to always work?? I could see getting a 0x0 out of this if I was using const cast or dynamic cast. what gives? Why doesn't this cast evaluate as a a simple pointer copy?
I am confused about your code and "I need the cast because of the const that was declared in the base class on the pure virtual method dosomething()." Why do you need to cast when the this pointer is already a constant pointer to a MyDerivedType and effectively a constant pointer to a constant MyDerivedType in this function?
Quote:
Aren't the old style pointer casts supposed to always work?
You really should be using C++ casts when and if they are needed.
Maybe you could explain why this is needed and what you are trying to achieve.
I am confused about your code and "I need the cast because of the const that was declared in the base class on the pure virtual method dosomething()." Why do you need to cast when the this pointer is already a constant pointer to a MyDerivedType and effectively a constant pointer to a constant MyDerivedType in this function?
the const on the method declaration effectively make the this pointer a const MyDerivedType *this when accessed inside of dosomething(). So if I try to copy the this pointer without a cast the compiler complains because getting a pointer to the const object violates the const-ness.
typically one would use a const_cast here but I had done that and was finding the same erratic behavior, so I fell back on the old style cast because according to the docs these aren't supposed to return NULL...
effectively a constant pointer to a constant MyDerivedType in this function
not "a const MyDerivedType *" and I'm sorry I really have no idea what you are trying to achieve, besides trying to break the standard as what you are trying to do when using c style casts I would assume to be undefined behavior.
mutable is used in situations like this Hivemind when a constant function needs to change the class or it's members, but it can not be applied to the this pointer.
As I said santana can you please explain what you are trying to do, ie not just cast the pointer but why and for what purpose, then maybe we maybe able to help.
[edit edit]
Right I see what you are getting at then Hivemind, and yes my edit is asking the same thing, more information, tho I wasn't so brutal
I wasn't suggesting that either. I was suggesting mutable because with that maybe the derived implementation can *remain* const. But not knowing exactly what the OP wants to do it's hard to give good advice, sounds a bit like poor design.
dmail you sound like a dick, if you don't have the answer, or at least something constructive, then why bother posting?
Getting straight to the point doesn't necessarily make someone unhelpful. Also, remember that if the answers were only yours they wouldn't be posted in a public forum. You may start the topic, but ultimately the thread doesn't belong to you.
Casting pointers with () is inherited directly from C and merely tells the compiler that you know what you are doing and not to give a compile error. Nothing else normally happens. In other words, you are pretty much telling the compiler to ignore the conversion you are making no matter how little sense it makes, but it doesn't change the output code at all. You should not get 0x0 from a this pointer in this situation unless you are actually dereferencing NULL to call the function. The C cast and implicit removal of const qualifier shouldn't cause that.
The code you posted has inherent problems, but none of them cause a this pointer to be NULL. Your error originates from another part of your code. I'd start by looking at the particular instance for which this function is called and examine its value from declaration all the way past the function call.
ta0kira
NOTE: there is an extremely unlikely error that can occur with const_cast. It only happens when three very rare things are combined at the same time: a data member that ought to be mutable (such as is discussed above), a compiler that doesn't support the mutable keyword, and an object that was originally defined to be const (as opposed to a normal, non-const object that is pointed to by a pointer-to-const). Although this combination is so rare that it may never happen to you, if it ever did happen the code may not work (the Standard says the behavior is undefined).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.