object and class name reference

Discussion in 'C++' started by Gary Wessle, Nov 12, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    can I reference the class name from within the object?

    thanks
     
    Gary Wessle, Nov 12, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Salt_Peter Guest

    Gary Wessle wrote:
    > Hi
    >
    > can I reference the class name from within the object?
    >
    > thanks


    yes, typeid()...

    #include <iostream>
    #include <typeinfo>

    struct A {
    virtual ~A() { }
    };

    struct B : public A {
    };

    int main()
    {
    B b;
    A& r_a = b;

    std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
    }

    Note: If you remove the virtual d~tor, you loose.
     
    Salt_Peter, Nov 12, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Kavya Guest

    Salt_Peter wrote:
    > Gary Wessle wrote:
    > > Hi
    > >
    > > can I reference the class name from within the object?
    > >
    > > thanks

    >
    > yes, typeid()...
    >
    > #include <iostream>
    > #include <typeinfo>
    >
    > struct A {
    > virtual ~A() { }
    > };
    >
    > struct B : public A {
    > };
    >
    > int main()
    > {
    > B b;
    > A& r_a = b;
    >
    > std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
    > }
    >
    > Note: If you remove the virtual d~tor, you loose.


    What do you mean by "if you remove virtual destructor you lose"?
     
    Kavya, Nov 12, 2006
    #3
  4. Gary Wessle

    Salt_Peter Guest

    Kavya wrote:
    > Salt_Peter wrote:
    > > Gary Wessle wrote:
    > > > Hi
    > > >
    > > > can I reference the class name from within the object?
    > > >
    > > > thanks

    > >
    > > yes, typeid()...
    > >
    > > #include <iostream>
    > > #include <typeinfo>
    > >
    > > struct A {
    > > virtual ~A() { }
    > > };
    > >
    > > struct B : public A {
    > > };
    > >
    > > int main()
    > > {
    > > B b;
    > > A& r_a = b;
    > >
    > > std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
    > > }
    > >
    > > Note: If you remove the virtual d~tor, you loose.

    >
    > What do you mean by "if you remove virtual destructor you lose"?


    I'ld kindly suggest trying it.
    Without a virtual function in type A above, the class hierarchy would
    no longer be polymorphic.
    The typeid would identify the referenced object as type A erroneously.

    Perhaps the following helps, same outcome:

    int main()
    {
    B b;
    A* const p_a = &b; // const pointer

    std::cout << "type of r_a: " << typeid(*p_a).name() << std::endl;
    }
     
    Salt_Peter, Nov 12, 2006
    #4
  5. Gary Wessle

    Kai-Uwe Bux Guest

    Salt_Peter wrote:

    >
    > Gary Wessle wrote:
    >> Hi
    >>
    >> can I reference the class name from within the object?
    >>
    >> thanks

    >
    > yes, typeid()...
    >
    > #include <iostream>
    > #include <typeinfo>
    >
    > struct A {
    > virtual ~A() { }
    > };
    >
    > struct B : public A {
    > };
    >
    > int main()
    > {
    > B b;
    > A& r_a = b;
    >
    > std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
    > }


    On my machine, this prints:

    type of r_a: 1B

    Does not look exactly like the classname to me.

    The standard would allow

    #include <iostream>
    #include <typeinfo>
    struct Mickey_Mouse {};
    int main() {
    std::cout << typeid(Mickey_Mouse).name() << std::endl;
    }

    to print Donald_Duck.

    [snip]


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 12, 2006
    #5
  6. Gary Wessle

    Diwa Guest

    Did some research on this.
    See inline.

    Kai-Uwe Bux wrote:

    > > #include <iostream>
    > > #include <typeinfo>
    > >
    > > struct A {
    > > virtual ~A() { }
    > > };
    > >
    > > struct B : public A {
    > > };
    > >
    > > int main()
    > > {
    > > B b;
    > > A& r_a = b;
    > >
    > > std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
    > > }

    >
    > On my machine, this prints:
    >
    > type of r_a: 1B
    >
    > Does not look exactly like the classname to me.
    >


    If on linux, do "c++filt 1B"
    This will give unmangled name i.e. "B"

    -- Diwakar
     
    Diwa, Nov 22, 2006
    #6
  7. Diwa wrote:
    > Did some research on this.
    > See inline.
    >
    > Kai-Uwe Bux wrote:
    >
    >>> #include <iostream>
    >>> #include <typeinfo>
    >>>
    >>> struct A {
    >>> virtual ~A() { }
    >>> };
    >>>
    >>> struct B : public A {
    >>> };
    >>>
    >>> int main()
    >>> {
    >>> B b;
    >>> A& r_a = b;
    >>>
    >>> std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
    >>> }

    >>
    >> On my machine, this prints:
    >>
    >> type of r_a: 1B
    >>
    >> Does not look exactly like the classname to me.
    >>

    >
    > If on linux, do "c++filt 1B"
    > This will give unmangled name i.e. "B"


    Platform-specific information is off-topic.

    The 'name' member function of 'type_info' class is NOT guaranteed to
    return anything meaningful. It's all compiler-specific and off-topic.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 22, 2006
    #7
  8. Gary Wessle

    Diwa Guest

    Victor Bazarov wrote:

    > >>> #include <iostream>
    > >>> #include <typeinfo>
    > >>>
    > >>> struct A {
    > >>> virtual ~A() { }
    > >>> };
    > >>>
    > >>> struct B : public A {
    > >>> };
    > >>>
    > >>> int main()
    > >>> {
    > >>> B b;
    > >>> A& r_a = b;
    > >>>
    > >>> std::cout << "type of r_a: " << typeid(r_a).name() << std::endl;
    > >>> }
    > >>
    > >> On my machine, this prints:
    > >>
    > >> type of r_a: 1B
    > >>

    > >
    > > If on linux, do "c++filt 1B"
    > > This will give unmangled name i.e. "B"

    >
    > Platform-specific information is off-topic.
    >
    > The 'name' member function of 'type_info' class is NOT guaranteed to
    > return anything meaningful. It's all compiler-specific and off-topic.
    >

    Yes, you are right and anyway "c++filt" is a cmd line program
    and hence not usable in a C++ program even if its for Linux.
     
    Diwa, Nov 22, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    2
    Views:
    2,852
    ding feng
    Jun 25, 2003
  2. Replies:
    10
    Views:
    543
    Jim Langston
    May 25, 2006
  3. Replies:
    1
    Views:
    489
    Mark Rae [MVP]
    Sep 20, 2007
  4. tenxian
    Replies:
    7
    Views:
    361
    RedGrittyBrick
    Apr 23, 2008
  5. Peng Yu
    Replies:
    5
    Views:
    100
    Jens Thoms Toerring
    Jun 9, 2010
Loading...

Share This Page