C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

typeid and dynamic_cast, gcc 3.3
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Andreas Sch.
Guest





PostPosted: Fri Jan 23, 2004 9:43 am    Post subject: typeid and dynamic_cast, gcc 3.3 Reply with quote



Hello,

I had been quite suprised by the
following little program:

---- cut ----

#include "iostream.h"

class base {
  public:
  base(){}
  virtual ~base(){};
  void printType(base * obj);
};

class derived : public base {
  public:
  virtual void doIt()
  {
    printType(this);
  }  
};

void base::printType(base * obj) {
  cout << "type is " << typeid(obj).name() << endl;
  cout << dynamic_cast }

int main(int argc, char * argv[]) {
  derived myObj;
  myObj.doIt();
}

---- cut ----

It prints:

type is P4base
0xbfffec00

My expectation was to see "derived" for
the type name.

And even if it is identified as "base"
the RTTI should not be able to dynamic-cast
it to a derived object which it is reported
not to be.

Tested with gcc 3.3 and 3.3.2.

Somebody able to enlighten me?

Greetings,
Andreas
Back to top
Asfand Yar Qazi
Guest





PostPosted: Fri Jan 23, 2004 10:06 am    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote



Andreas Sch. wrote:
Quote:
Hello,

I had been quite suprised by the
following little program:

---- cut ----

#include "iostream.h"

class base {
public:
base(){}
virtual ~base(){};
void printType(base * obj);
};

class derived : public base {
public:
virtual void doIt()
{
printType(this);
}
};

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


Quote:
cout << dynamic_cast

I don't see the purpose of doing this: it just prints out the pointer
value. Did you forget to typeid() it?

Quote:
}

int main(int argc, char * argv[]) {
derived myObj;
myObj.doIt();
}

---- cut ----



--
http://www.it-is-truth.org/


Back to top
Gianni Mariani
Guest





PostPosted: Fri Jan 23, 2004 10:13 am    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote



Andreas Sch. wrote:
Quote:
Hello,

I had been quite suprised by the
following little program:

---- cut ----

#include "iostream.h"
this header is not standard - use #include <iostream


Quote:

class base {
public:
base(){}
virtual ~base(){};
^^^^^^^^ ';' not needed


Quote:
void printType(base * obj);
};

class derived : public base {
public:
virtual void doIt()
{
printType(this);
}
};

void base::printType(base * obj) {
cout << "type is " << typeid(obj).name() << endl;

^^^^^^^^^^^^^^^ ... this is the same as

cout << "type is " << typeid( (base *) 0 ).name() << endl;



Quote:
cout << dynamic_cast }

int main(int argc, char * argv[]) {
derived myObj;
myObj.doIt();
}

---- cut ----

It prints:

type is P4base
0xbfffec00

My expectation was to see "derived" for
the type name.

And even if it is identified as "base"
the RTTI should not be able to dynamic-cast
it to a derived object which it is reported
not to be.

Tested with gcc 3.3 and 3.3.2.

Somebody able to enlighten me?

Try this:

class derived;

#include using namespace std;

class base {
public:
base(){}
virtual ~base(){}

template <typename T>
void printType(T * obj)
{
cout << "type is " << typeid(obj).name() << endl;
cout << dynamic_cast }
};

class derived : public base {
public:
virtual void doIt()
{
printType(this);
}
};


int main(int argc, char * argv[]) {
derived myObj;
myObj.doIt();
}


Back to top
Andreas Sch.
Guest





PostPosted: Fri Jan 23, 2004 10:41 am    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote

Asfand Yar Qazi wrote:
Quote:
(...)
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).

Quote:

cout << dynamic_cast
I don't see the purpose of doing this: it just prints out the pointer
value. Did you forget to typeid() it?


I just wanted to see if the dynamic_cast fails and
delivers 0x0. I expected it to fail since the typeid-line
printed "base" and not "derived".

Thanks for your answer!
Andreas

Back to top
Andreas Sch.
Guest





PostPosted: Fri Jan 23, 2004 10:55 am    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote

Gianni Mariani wrote:
Quote:
(...)
#include "iostream.h"
this header is not standard - use #include

Ok...

Quote:

class base {
(...)
virtual ~base(){};
^^^^^^^^ ';' not needed


Oops, sure.

Quote:
(...)
void base::printType(base * obj) {
cout << "type is " << typeid(obj).name() << endl;

^^^^^^^^^^^^^^^ ... this is the same as

cout << "type is " << typeid( (base *) 0 ).name() << endl;


So typeid delivers the type of the pointer,
not the object.

Quote:
(...)
Try this:
(...)

That worked. Thank you.

I have been hunting a strange problem in a program
which fails at a dynamic_cast. To debug that situation
I heavily used typeid() and was quite suprised about
its different behavior.

If I am able to shorten the program to a small
example and still produce the same behavior I'll
write that here, too.

Greetings,
Andreas

Back to top
Sharad Kala
Guest





PostPosted: Fri Jan 23, 2004 11:30 am    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote


"Andreas Sch." <no_reply (AT) yahoo (DOT) com> wrote

Quote:
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 -
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 ;-)



Back to top
Sharad Kala
Guest





PostPosted: Fri Jan 23, 2004 1:33 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote


"Sharad Kala" <no.spam_sharadk_ind (AT) yahoo (DOT) com> wrote

Quote:

/snip

You don't seem to include typeinfo in your code.

Quote from Section 5.2.8.6 of the standard.
" If the header <typeinfo> is not included prior to a use of typeid, the program
is ill-formed. "

Best wishes,
Sharad



Back to top
Rolf Magnus
Guest





PostPosted: Fri Jan 23, 2004 3:53 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote

Andreas Sch. wrote:

Quote:
void base::printType(base * obj) {
cout << "type is " << typeid(obj).name() << endl;

^^^^^^^^^^^^^^^ ... this is the same as

cout << "type is " << typeid( (base *) 0 ).name() << endl;


So typeid delivers the type of the pointer,
not the object.

Yes. That's what you asked for. If you provide a pointer to typeid(), it
will tell you that it's a pointer Wink
If you want info about what the pointer points to, you need to
dereference, i.e typeid(*obj).


Back to top
Andreas Sch.
Guest





PostPosted: Thu Jan 29, 2004 3:44 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote

Hi!

First of all, thank you all for your valuable
answers to my previous question!

Again, I have a short program illustrating my
(next) problem:

--- snipp ---

#include <typeinfo>
#include <iostream>

class Base
{
public:
Base(){};
virtual ~Base(){};
void calledMethod( Base * a_Base );
};

class Derived : Base
{
public:
Derived() : Base(){};
virtual ~Derived(){};
virtual void callingMethod();
};

void Base::calledMethod( Base * a_base )
{
std::cout << "Parameter is of type "" << typeid(*a_base).name()
<< "" at address " << dynamic_cast << std::endl;
}

void Derived::callingMethod( )
{
std::cout << "I am of type "" << typeid(*this).name()
<< "" at address " << dynamic_cast << std::endl;
calledMethod( this );
}

int main(int argc, char * argv[])
{
Derived myDerived;
myDerived.callingMethod();
}

/* Program output:

I am of type "7Derived" at address 0xbfffea90
Parameter is of type "7Derived" at address 0

*/

--- snipp ---

The question is: Why is the dynamic_cast in calledMethod()
failing?

RU,
Andreas
Back to top
Ron Natalie
Guest





PostPosted: Thu Jan 29, 2004 4:25 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote


"Andreas Sch." <no_reply (AT) yahoo (DOT) com> wrote

Quote:
class Derived : Base

You want
class Derived : public Base

I can't even understand why this compiles.



Back to top
Rolf Magnus
Guest





PostPosted: Thu Jan 29, 2004 5:09 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote

Ron Natalie wrote:

Quote:
class Derived : Base

You want
class Derived : public Base

I can't even understand why this compiles.

I don't know whether it should compile or not, but this probably is the
reason why it doesn't work. If you derive virtually, Base is not an
accessible base class of Derived.


Back to top
Ron Natalie
Guest





PostPosted: Thu Jan 29, 2004 5:50 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote

Quote:
Ron Natalie wrote:

class Derived : Base

You want
class Derived : public Base

I can't even understand why this compiles.

I don't know whether it should compile or not, but this probably is the
reason why it doesn't work. If you derive virtually, Base is not an
accessible base class of Derived.

You mean privately, not virtually.


The program is ill-formed because you can't call the base class "calledMethod"
function since it's not accessible.

Back to top
Michael Mellor
Guest





PostPosted: Thu Jan 29, 2004 6:06 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote

Ron Natalie wrote:
Quote:
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote


Ron Natalie wrote:


class Derived : Base

You want
class Derived : public Base

I can't even understand why this compiles.

I don't know whether it should compile or not, but this probably is the
reason why it doesn't work. If you derive virtually, Base is not an
accessible base class of Derived.


You mean privately, not virtually.

The program is ill-formed because you can't call the base class "calledMethod"
function since it's not accessible.
It is not ill-formed, to quote 11.2.1 from the standard:

"...If a class is declared to be a base class for another class using
the private access specifier, the public and protected members of the
base class are accessible as private members of the derived class."

Therefore the following code is legal:
class A {
public:
void A_f ( ) { }
};

class B : A {
public:
void f ( ) { A_f ( ); }
};

Michael Mellor

Back to top
Rolf Magnus
Guest





PostPosted: Thu Jan 29, 2004 6:07 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote

Ron Natalie wrote:

Quote:

"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote in message
news:bvbf9b$v87$05$1 (AT) news (DOT) t-online.com...
Ron Natalie wrote:

class Derived : Base

You want
class Derived : public Base

I can't even understand why this compiles.

I don't know whether it should compile or not, but this probably is
the reason why it doesn't work. If you derive virtually, Base is not
an accessible base class of Derived.

You mean privately, not virtually.

Of course. Sorry.

Quote:
The program is ill-formed because you can't call the base class
"calledMethod" function since it's not accessible.

calledMethod is only called by Derived::callingMethod, from where it is
accessible.


Back to top
Ron Natalie
Guest





PostPosted: Thu Jan 29, 2004 7:23 pm    Post subject: Re: typeid and dynamic_cast, gcc 3.3 Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote

Quote:
calledMethod is only called by Derived::callingMethod, from where it is
accessible.

It is NOT accessible. Base is inherited privately. NONE of it's members
are accessible.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.