Re: typeid and dynamic_cast, gcc 3.3

From: Sharad Kala (no.spam_sharadk_ind_at_yahoo.com)
Date: 01/23/04


Date: Fri, 23 Jan 2004 17:00:12 +0530


"Andreas Sch." <no_reply@yahoo.com> wrote in message
news:buqtp6$l9b28$1@ID-152588.news.uni-berlin.de...
> Asfand Yar Qazi wrote:
> >> (...)
> >> void base::printType(base * obj) {
> >> cout << "type is " << typeid(obj).name() << endl;
> >
> > This isn't a virtual function, so it will be called with type of 'this'
> > set to "base", even though it was called from a virtual function
> > (virtual-ness doesn't pass along the calling sequence, its on a
> > per-function basis.) Dynamic-casting
> >
>
> Lets see if I got it: typeid does not evaluate the
> type information in memory but depends on the type
> of the "obj" pointer (and not on what it is pointing to).

This is a quote from msdn -
<Quote>
If the expression points to a base class type, yet the object is actually of a
type derived from that base class, a type_info reference for the derived class
is the result. The expression must point to a polymorphic type (a class with
virtual functions). Otherwise, the result is the type_info for the static class
referred to in the expression. Further, the pointer must be dereferenced so that
the object it points to is used. Without dereferencing the pointer, the result
will be the type_info for the pointer, not what it points to.
</Quote>

Best wishes,
Sharad

Disclaimer : I know msdn isn't totally accurate at places but not here ;-)



Relevant Pages

  • Re: the virtual keyword
    ... > you could not declare a body for it in the class it was declared virtual, ... This class can only be used as a base class, ... would result in a compiler error. ... function pointer that is added to the actual class. ...
    (comp.lang.cpp)
  • Re: Status of a 2.6.30 kernel ? Other sources for a 2.6.30 kernel.
    ... Deferencing a null pointer is an undefined operation. ... Dereferencing a NULL pointer is undefined. ... On other systems, the optimization is correct. ... What I don't understand is why the kernel isn't oopsing on the ...
    (Fedora)
  • Re: Design problem
    ... Inspect the pointer, if it points to a Frog (or something that implements ... Or to stay with the example: Introducing a spline item. ... >> nor to have a fat interface at the base class. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Validate a pointer
    ... > if a pointer one is abouty to dereference is NULL, ... dereferencing NULL is a bug. ... > so dereferencing a NULL pointer would NOT cause a core dump. ...
    (comp.unix.programmer)
  • Re: if (pointer) ?
    ... There have been systems where dereferencing a null ... early MS-DOS machines had this characteristic). ... null pointer on such machines could cause seriously unexpected ... NULL has a very special meaning, one which doesn't fit this context. ...
    (comp.lang.c)

Loading