speed of casts

  • Thread starter Mike -- Email Ignored
  • Start date
M

Mike -- Email Ignored

class Base
{
....
}
class Child : public BASE
{
public:
Thing& getThing();
....
}

Base* pBase = new Child;

Base* qBase = pBase;

Now if I verify that:

dynamic_cast<Child*>(qBase) != 0

and I have a lot of dereferencing to do, I
imagine that the code would be faster in I use,
for example:

Thing thing = reinterpret_cast<Child*>(qBase)->getThing();

instead of the usual dynamic_cast.

Is this correct?

Thanks,
Mike.
 
J

joecook

Now if I verify that:

    dynamic_cast<Child*>(qBase) != 0

and I have a lot of dereferencing to do, I
imagine that the code would be faster in I use,
Thing  thing = reinterpret_cast<Child*>(qBase)->getThing();

instead of the usual dynamic_cast.

Is this correct?

"faster" is relative, but dynamic cast does more checking.
Instead of dynamic_cast, you should replace it with static_cast, not
reinterpret_cast in this case.

Joe C
 
A

Alf P. Steinbach

* Mike -- Email Ignored:
class Base
{
...
}
class Child : public BASE
{
public:
Thing& getThing();
...
}

Base* pBase = new Child;

Base* qBase = pBase;

Now if I verify that:

dynamic_cast<Child*>(qBase) != 0

and I have a lot of dereferencing to do, I
imagine that the code would be faster in I use,
for example:

Thing thing = reinterpret_cast<Child*>(qBase)->getThing();

instead of the usual dynamic_cast.

Perhaps. Measure.

Is this correct?

No, and therein lies the rub.


Cheers & hth.,

- Alf
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top