LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2007, 02:42 PM   #1
santana
Member
 
Registered: Sep 2004
Distribution: FC, ubuntu, OpenSuse
Posts: 112

Rep: Reputation: 16
pointer cast c++


Hi, I am pulling my hair out(& soon will be bald)

I have a very strange situation. I have a derived c++ class, where in base class there is a pure virtual method declared as

void dosomething() const;

within my implementation of dosomething() I attempting to copy the this pointer like:

void MyDerivedType::dosomething() const
{

MyDerivedType *p = (MyDerivedType *)this;
assert(p);

}

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?

 
Old 10-17-2007, 03:23 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 10-17-2007 at 03:24 PM.
 
Old 10-17-2007, 03:46 PM   #3
santana
Member
 
Registered: Sep 2004
Distribution: FC, ubuntu, OpenSuse
Posts: 112

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by dmail View Post
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...
 
Old 10-17-2007, 03:51 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
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.
 
Old 10-17-2007, 03:53 PM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Have a look at the keyword mutable, maybe it will enable you to do what you want. Without any casts.
 
Old 10-17-2007, 03:55 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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

Last edited by dmail; 10-17-2007 at 03:59 PM.
 
Old 10-17-2007, 03:57 PM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 10-17-2007, 04:09 PM   #8
santana
Member
 
Registered: Sep 2004
Distribution: FC, ubuntu, OpenSuse
Posts: 112

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by dmail View Post
...
dmail you sound like a dick, if you don't have the answer, or at least something constructive, then why bother posting?
 
Old 10-17-2007, 04:14 PM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by santana View Post
dmail you sound like a dick, if you don't have the answer, or at least something constructive, then why bother posting?
Thank you for the complement, go and help yourself.
 
Old 10-18-2007, 07:02 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by santana View Post
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
 
Old 10-19-2007, 07:55 AM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
The code you posted has inherent problems, but none of them cause a this pointer to be NULL
For the reason of thread completeness I post here again. There is a case when this can result in undefined behavior

http://www.parashift.com/c++-faq-lit...html#faq-18.13
Quote:
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).

Last edited by dmail; 10-19-2007 at 07:57 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cast from pointer to integer of different size: what is the problem??? bobo333 Linux - Software 2 08-27-2006 09:59 PM
makes pointer from integer without a cast ? hubabuba Programming 2 01-28-2005 05:28 PM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
makes pointer from interger without a cast y0shi Linux - General 4 10-21-2004 08:17 PM
Passing arg 3 of blah makes pointer from integer without a cast xconspirisist Programming 6 08-22-2004 08:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration