Solved

casting pointer to static

Posted on 2001-06-11
13
245 Views
Last Modified: 2011-09-20
i m not sure if i should post this question here or on the MFC board, but lets try :-)
i have the following:
Class A
{
....
};
class B : public A
{
...
};

void main (voif)
{
  A* a = new A;
  B  b = static_cast<B>a;
}

i was wondering if i can do that ? ofcourse it doesn't work, but is there's a way to do that ?

Shay
0
Comment
Question by:Shay050799
  • 3
  • 2
  • 2
  • +5
13 Comments
 
LVL 30

Expert Comment

by:Zoppo
ID: 6177755
What should this code do? Cast a pointer to an instance of a base class to an instance of a
derived class ... what should 'b' be after that cast?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6177761
u r trying to cast a pointer to class A to an instance of class b, of course it won't work, check this code below:

      A* pa = new A;

      B* pb = static_cast<B*>(pa);

now its gonna work...

good luck
0
 
LVL 3

Author Comment

by:Shay050799
ID: 6177774
that works but pb is a pointer
i need to refer to it using -> operator
i want a static instance of B

Shay
0
 
LVL 30

Expert Comment

by:Axter
ID: 6177788
sedgwick,
I think he's trying to make b equal to a.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6177799
A* a = new A;
 B  b = static_cast<B>*a;
0
 
LVL 30

Expert Comment

by:Axter
ID: 6177806
If you want b equal to the value of a, then you need to use the asteric.
*a
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 42

Expert Comment

by:sedgwick
ID: 6177955
shay:
>>i want a static instance of B
so why dont u declare a static instance of B as follow:

static B b;

what is the purpose of the casting from A* ???
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 6177983
you could write a B::operator = (A& a) and then call

static B b;
b = *a;

or write a ctor B::(A& a) and then call

static B b(a);
//or
static B b = a;

ZOPPO
0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 50 total points
ID: 6178728
You can not cast a pointer to A to a pointer to B, using static_cast.
static_cast verifies at compile time wheter a cast is legal or not. Casting a pointer to a base class object into a pointer to a derived class object is /never/ legal. And even if it were, it would be very, very unsafe:
class A {
   int i;
   // whatever
};

class B : public A
{
  int j;
  public: void set(int jj) { j = jj; }  
};

A* a = new A;
B* b = magic_cast<B*>(a);
b->set(42); // Now you've done it, deamons flying all over the place.

But of course, C++ wouldn't be C ++ if there wouldn't be ways to cast some magic and get them daemons out flying of yer nose.

First, there is
   reinterpret_cast<>
which more or less allows you to cast anything into anything else (well, more or less, it won't let you cast functionpointer into memberfunctionpointers and the likes).

Then there is an old C union trick, something like (I might have the syntax wrong, I never use unions):
union{
   A* a;
   B* b;
};

The following code demonstates them:

struct A {};
struct B : A {int i;};

void f()
{
   A* a = new A;
   B* b = reinterpret_cast<B*>(a);

   union{
      A* a;
      B* b;
   } gotcha;

   gotcha.a = a;
   gotcha.b->i = 42; // Welcome deamons! And we didn't even use magic :)

}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6179041
Like I said, this is 'dangerous' to do and will easily lead to 'undefined' behaviour of your program, or crashes (undefined behaviour is what the Standard calls it).

What do you need it for?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6799465
Shay----->  You have asked 95 questions at this site, and only awarded 53 of them....  this is totally against the Guidelines of this site.  Please return to them and update/finalize them this week.  The list is below.  Of these 53 awarded, there are 7 questions that are -1 user id problems which we will need to manually correct.  Please accept the first correct response in each of them and award the experts who helped you with the deserved "A" grade, or comments if less than an A as to why.  If you need our help to special handle any of these, let us know that as well via comment here.  If these are not finalized this week, I will contact Administration for further action.  Please click the HELP DESK link on the left for the rules of engagement here (Guidelines, Member Agreement and Question/Answer process).

I also noticed that a number of your questions are less than 50 points, which is the recommended value for EASY questions, with 300 points the maximum.  To draw additional attention, you can always increase their values without our assistance.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20229485
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20123800
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20121605
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20120473
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20110164
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20109749
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20077855
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=12019739 November, 2000, zero comments added, 5 points.
http://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=11169491 November, 2000 ZERO points, comments added
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20135373
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20133207
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20115081
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20103624
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20102120
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20100568
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20088761
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20076668
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=11536619 October, 2000 - 15 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=12014539  November, 2000
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11990378      "
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11863978      "
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20223052
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20220148
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20149200  July, 2001 - 5 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20148518    "      - 10 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20138848
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20137234
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20132473
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20132221
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20122930  May, 2001 5 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20121466  May, 2001 0 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20094887
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20094102
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20067934

I have asked Administration to Email you in these regards.  I also noticed several other things I'd like to bring to you attention.  There are some that you did award with "C" grades, and no comment as to why.  Also, some of your questions have had zero response, and that is likely for two reasons:  Poor closure on open items and poor grades.  I think you'll agree, epecially given the number of questions you've asked here, that experts here take great pride in their work, and deserve "A" grades and responses to all help they've given you.

EXPERTS -->  In the event any of these items remain open at the end of this week, I will finalize them and look to you for guidance on the fair results.  As always, we very much appreciate your willingness to help and the excellent support you all give.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 11

Expert Comment

by:griessh
ID: 6814535
Losts of true comments, but no clarification of the problem ... I suggest to PAQ & refund.

Please do not accept this comment as an answer!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6822832
Admin notified of user neglect. Force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now