[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

dynamic cast questions

puzzlecracker

9/27/2008 3:43:00 PM



What about dynamic_cast when dynamic reference is not unique (e.g. A-
>B, B->C, B->D, is dynamic_cast<B *> correct for pointer to D?

Why would you use dynamic cast<void *>?

What is typeid & what does it return? How would you use it?

What is wrong here

void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);
7 Answers

Jeff Schwab

9/27/2008 3:50:00 PM

0

puzzle cracker wrote:
>
> What about dynamic_cast when dynamic reference is not unique (e.g. A-
>> B, B->C, B->D, is dynamic_cast<B *> correct for pointer to D?
>
> Why would you use dynamic cast<void *>?
>
> What is typeid & what does it return? How would you use it?
>
> What is wrong here
>
> void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);

Where have you actually seen any of these constructs? This smells like
homework.

Obnoxious User

9/27/2008 4:29:00 PM

0

On Sat, 27 Sep 2008 08:42:41 -0700, puzzlecracker wrote:

> What about dynamic_cast when dynamic reference is not unique (e.g. A-
>>B, B->C, B->D, is dynamic_cast<B *> correct for pointer to D?
>
> Why would you use dynamic cast<void *>?
>
> What is typeid & what does it return? How would you use it?
>
> What is wrong here
>
> void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);

Why don't you try google for it? Most of the time
that's a pretty fast way to get information and
answers.

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

puzzlecracker

9/27/2008 4:46:00 PM

0

On Sep 27, 11:50 am, Jeff Schwab <j...@schwabcenter.com> wrote:
> puzzle cracker wrote:
>
> > What about dynamic_cast when dynamic reference is not unique (e.g. A-
> >> B, B->C, B->D, is dynamic_cast<B *> correct for pointer to D?
>
> > Why would you use dynamic cast<void *>?
>
> > What is typeid & what does it return? How would you use it?
>
> > What is wrong here
>
> > void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);
>
> Where have you actually seen any of these constructs? This smells like
> homework.

lazy to google it, I found these guys at c++ trivia forum, I actually
have lots more of this intricate questions.

Not really in the need of solutions, just wanted you guys to see c++
from the unknown angle...

Obnoxious User

9/27/2008 4:54:00 PM

0

On Sat, 27 Sep 2008 09:46:04 -0700, puzzlecracker wrote:

> On Sep 27, 11:50 am, Jeff Schwab <j...@schwabcenter.com> wrote:
>> puzzle cracker wrote:
>>
>> > What about dynamic_cast when dynamic reference is not unique (e.g. A-
>> >> B, B->C, B->D, is dynamic_cast<B *> correct for pointer to D?
>>
>> > Why would you use dynamic cast<void *>?
>>
>> > What is typeid & what does it return? How would you use it?
>>
>> > What is wrong here
>>
>> > void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);
>>
>> Where have you actually seen any of these constructs? This smells like
>> homework.
>
> lazy to google it, I found these guys at c++ trivia forum, I actually
> have lots more of this intricate questions.
>
> Not really in the need of solutions, just wanted you guys to see c++
> from the unknown angle...

What unkown angle?

Are you trolling now?

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

James Kanze

9/27/2008 6:53:00 PM

0

On Sep 27, 5:42 pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> What about dynamic_cast when dynamic reference is not unique
> (e.g. A-> B, B->C, B->D, is dynamic_cast<B *> correct for
> pointer to D?

Obviously. So is static_cast, and any time static_cast is OK,
so is dynamic_cast.

But I don't understand the comments above. B is unique.
Perhaps you meant something like:

B B
| |
L R
\ /
D

D* pD = new D ;
B* pB = dynamic_cast< B* >( pD ) ;

Most people would probably try static_cast in such cases, but
the results are the same, static_cast or dynamic_cast: the
conversion is ambiguous, and thus rejected by the compiler.

Perhaps a more complete example of what you're asking would be
in order. Or just read the exact rules (§5.2.7/8 in the
standard).

> Why would you use dynamic cast<void *>?

Because you want the void* to point to the complete object, and
not just to the sub-object pointed to by the pointer you have.

> What is typeid

An operator.

> & what does it return?

Since it's not a function, it doesn't return anything. The
result of the operator is an lvalue of type std::type_info
const, or of some implementation defined type derived from
std::type_info (also const).

> How would you use it?

Depends on what you want to use it for.

> What is wrong here

> void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);

It's not legal C++. You can't dynamic_cast a void*.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

9/27/2008 6:56:00 PM

0

On Sep 27, 6:28 pm, Obnoxious User <O...@127.0.0.1> wrote:
> On Sat, 27 Sep 2008 08:42:41 -0700, puzzlecracker wrote:
> > What about dynamic_cast when dynamic reference is not unique (e.g. A-
> >>B, B->C, B->D, is dynamic_cast<B *> correct for pointer to D?

> > Why would you use dynamic cast<void *>?

> > What is typeid & what does it return? How would you use it?

> > What is wrong here

> > void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);

> Why don't you try google for it? Most of the time
> that's a pretty fast way to get information and
> answers.

So is asking questions here. With the advantage that 1) you
don't have to figure out how to enter your request (what
keywords should he google for here), and 2) there's actually a
reasonable chance that you get the right answer, and can
recognize it as such.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Hendrik Schober

9/27/2008 11:24:00 PM

0

James Kanze wrote:
> On Sep 27, 6:28 pm, Obnoxious User <O...@127.0.0.1> wrote:
> [...]
>> Why don't you try google for it? Most of the time
>> that's a pretty fast way to get information and
>> answers.
>
> So is asking questions here. With the advantage that 1) you
> don't have to figure out how to enter your request (what
> keywords should he google for here), and 2) there's actually a
> reasonable chance that you get the right answer, and can
> recognize it as such.

3) You might learn much more than just the answer to what you asked.

4) So might we.

Schobi