dynamic cast vs reinterpret_cast

A

Asif Zaidi

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;
}
 
A

Asif Zaidi

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
 
V

Vladimir Jovic

Asif said:
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?
 
A

Asif Zaidi

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
 
V

Victor Bazarov

Asif said:
Asif said:
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
 
M

Martijn van Buul

* Asif Zaidi:
[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)
 
N

Noah Roberts

* 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.
 
J

James Kanze

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:).
 
V

Victor Bazarov

Juha said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top