Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynamic cast vs reinterpret_cast

Reply
Thread Tools

dynamic cast vs reinterpret_cast

 
 
Asif Zaidi
Guest
Posts: n/a
 
      02-09-2010
Hi:
I have the 2 classes below. I am trying to cast them such that output
should be different. But I am getting to be the same all the time.
Suggestions only plz. Thanks

class Base { public: virtual void foo() {} };
class Derived : public Base { public: virtual void foo() { cout << "
in derived" << endl; } };

int main()
{
Derived dobj;
Base *b = &dobj;

cout << dynamic_cast<Derived*>(b) << endl;
cout << reinterpret_cast<Derived*>(b) << endl;
}
 
Reply With Quote
 
 
 
 
Asif Zaidi
Guest
Posts: n/a
 
      02-09-2010
On Feb 9, 2:33*am, Asif Zaidi <(E-Mail Removed)> wrote:
> Hi:
> I have the 2 classes below. I am trying to cast them such that output
> should be different. But I am getting to be the same all the time.
> Suggestions only plz. Thanks
>
> class Base { public: * *virtual void foo() {} *};
> class Derived : public Base { public: * virtual void foo() { cout << "
> in derived" << endl; } };
>
> int main()
> {
> * * * * Derived dobj;
> * * * * Base *b = &dobj;
>
> * * * * cout << dynamic_cast<Derived*>(b) * * << endl;
> * * * * cout << reinterpret_cast<Derived*>(b) << endl;
>
> }
>
>


No need to respond plz... figured it out

Thanks

Asif
 
Reply With Quote
 
 
 
 
Vladimir Jovic
Guest
Posts: n/a
 
      02-09-2010
Asif Zaidi wrote:
> On Feb 9, 2:33 am, Asif Zaidi <(E-Mail Removed)> wrote:
>> Hi:
>> I have the 2 classes below. I am trying to cast them such that output
>> should be different. But I am getting to be the same all the time.
>> Suggestions only plz. Thanks
>>
>> class Base { public: virtual void foo() {} };
>> class Derived : public Base { public: virtual void foo() { cout << "
>> in derived" << endl; } };
>>
>> int main()
>> {
>> Derived dobj;
>> Base *b = &dobj;
>>
>> cout << dynamic_cast<Derived*>(b) << endl;
>> cout << reinterpret_cast<Derived*>(b) << endl;
>>
>> }
>>
>>

>
> No need to respond plz... figured it out


Sorry for responding, but I am really wondering what you figured out.
The code you posted gives lots of compiling errors.

1) Doing the reinterpret_cast from the base class to the derived class
makes no sense (is it working at all?).
2) Both classes have no stream operator defined, so how can you pass
them to cout?
 
Reply With Quote
 
Asif Zaidi
Guest
Posts: n/a
 
      02-09-2010
On Feb 9, 3:52*am, Vladimir Jovic <(E-Mail Removed)> wrote:
> Asif Zaidi wrote:
> > On Feb 9, 2:33 am, Asif Zaidi <(E-Mail Removed)> wrote:
> >> Hi:
> >> I have the 2 classes below. I am trying to cast them such that output
> >> should be different. But I am getting to be the same all the time.
> >> Suggestions only plz. Thanks

>
> >> class Base { public: * *virtual void foo() {} *};
> >> class Derived : public Base { public: * virtual void foo() { cout << "
> >> in derived" << endl; } };

>
> >> int main()
> >> {
> >> * * * * Derived dobj;
> >> * * * * Base *b = &dobj;

>
> >> * * * * cout << dynamic_cast<Derived*>(b) * * << endl;
> >> * * * * cout << reinterpret_cast<Derived*>(b) << endl;

>
> >> }

>
> > No need to respond plz... figured it out

>
> Sorry for responding, but I am really wondering what you figured out.
> The code you posted gives lots of compiling errors.
>
> 1) Doing the reinterpret_cast from the base class to the derived class
> makes no sense (is it working at all?).
> 2) Both classes have no stream operator defined, so how can you pass
> them to cout?


Hi

1) Yes.. I wanted to get a different address but am getting the same
address with above code. I thought i had it figured out but apparently
not.
2) I am just printing out the address that it is pointing to.

I am using a Microsoft Visual 2008 C++ compiler. The code did compile
and run and i Saw the output.

Thanks

Asif
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-09-2010
Asif Zaidi wrote:
> On Feb 9, 3:52 am, Vladimir Jovic <(E-Mail Removed)> wrote:
>> Asif Zaidi wrote:
>>> [..]

>> Sorry for responding, but I am really wondering what you figured out.
>> The code you posted gives lots of compiling errors.
>>
>> [..]

>
> I am using a Microsoft Visual 2008 C++ compiler. The code did compile
> and run and i Saw the output.


Just a few words of caution: make sure when you set up your VC++
projects for the purposes of learning C++, that you disable language
extensions (a setting somewhere among C++ Language options). That will
likely prevent you from building Windows executables (Windows SDK
headers are full of code requiring the extensions), but at least you (a)
can build Console exes, and (b) you will be much closer to the real
language than to the MS dialect of it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Martijn van Buul
Guest
Posts: n/a
 
      02-09-2010
* Asif Zaidi:
>> >> class Base { public: ? ?virtual void foo() {} ?};
>> >> class Derived : public Base { public: ? virtual void foo() { cout << "
>> >> in derived" << endl; } };


[snip]

> 1) Yes.. I wanted to get a different address but am getting the same
> address with above code. I thought i had it figured out but apparently
> not.


You won't. Why would you? There's no reason to.

Now, if you were using multiple inheritance, *then* you'd see a difference
(And then the reinterpret_cast<> or a C-stype cast will no longer work)

--
Martijn van Buul - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      02-09-2010
In article <(E-Mail Removed)>, (E-Mail Removed) says...
>
> * Asif Zaidi:
> >> >> class Base { public: ? ?virtual void foo() {} ?};
> >> >> class Derived : public Base { public: ? virtual void foo() { cout << "
> >> >> in derived" << endl; } };

>
> [snip]
>
> > 1) Yes.. I wanted to get a different address but am getting the same
> > address with above code. I thought i had it figured out but apparently
> > not.

>
> You won't. Why would you? There's no reason to.
>
> Now, if you were using multiple inheritance, *then* you'd see a difference
> (And then the reinterpret_cast<> or a C-stype cast will no longer work)


The C-style cast would still work because it would, knowing the types of
both pointers, use a static cast instead of a reinterpret cast. Thus,
so long as you're not attempting to cross-cast (don't believe c-style
casts ever resolve to dynamic cast) it will work.

If, on the other hand, we had a main something like so:

int main()
{
struct X;
struct Y;
X* get_x();

std::cout << (Y*)(get_x()) << std::endl;
}

assuming X and Y are sub/base related, the above c-style would do a
reinterpret cast because it doesn't know that static is possible, nor
how to do it. With the full definition of X and Y though it would do a
static.

That is, in my opinion, one of the strongest arguments against using c-
style casts. C-style casts are different casts depending on context and
it can get pretty hairy knowing what's what.

--
http://crazyeddiecpp.blogspot.com
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-09-2010
On 9 Feb, 16:06, Juha Nieminen <(E-Mail Removed)> wrote:
> Vladimir Jovic wrote:
> > 2) Both classes have no stream operator defined, so how can you pass
> > them to cout?


> If you give a pointer to cout, it will print the value of that
> pointer (ie usually the memory address it's pointing to).


Unless it's a pointer to a character type.

--
James Kanze
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-10-2010
Juha Nieminen wrote:
> James Kanze wrote:
>> On 9 Feb, 16:06, Juha Nieminen <(E-Mail Removed)> wrote:
>>> Vladimir Jovic wrote:
>>>> 2) Both classes have no stream operator defined, so how can you pass
>>>> them to cout?
>>> If you give a pointer to cout, it will print the value of that
>>> pointer (ie usually the memory address it's pointing to).

>> Unless it's a pointer to a character type.

>
> Assume you wanted to output the value of a char* rather than the
> string it's pointing to. What would be the "proper" way of doing that?


static_cast<void*>, probably.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-10-2010
On Feb 10, 7:54 pm, Juha Nieminen <(E-Mail Removed)> wrote:
> James Kanze wrote:
> > On 9 Feb, 16:06, Juha Nieminen <(E-Mail Removed)> wrote:
> >> Vladimir Jovic wrote:
> >>> 2) Both classes have no stream operator defined, so how can you pass
> >>> them to cout?


> >> If you give a pointer to cout, it will print the value of
> >> that pointer (ie usually the memory address it's pointing
> >> to).


> > Unless it's a pointer to a character type.


> Assume you wanted to output the value of a char* rather than
> the string it's pointing to. What would be the "proper" way of
> doing that?


Cast it to void*.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
Moving to static_cast and reinterpret_cast from old C-style cast Kobe C++ 3 02-15-2006 08:13 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments