How to find out object type - PLEASE HELP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gandu

    How to find out object type - PLEASE HELP

    Could some C++ guru please help me? Suppose I have a linked list, in which the
    data stored is of type T (using templates) or void* (as in C). Is there any
    way to find out the type of the object (other than type-casting) when the
    object is extracted fronm the list? Thanks in advance for your help.
  • Jakob Bieling

    #2
    Re: How to find out object type - PLEASE HELP

    "Gandu" <cpptutor2000@y ahoo.com> wrote in message
    news:3c7c9804.0 404171006.1e9fd d46@posting.goo gle.com...[color=blue]
    > Could some C++ guru please help me? Suppose I have a linked list, in which[/color]
    the[color=blue]
    > data stored is of type T (using templates) or void* (as in C). Is there[/color]
    any[color=blue]
    > way to find out the type of the object (other than type-casting) when the
    > object is extracted fronm the list? Thanks in advance for your help.[/color]

    Maybe the typeid operator is of help?
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • David Harmon

      #3
      Re: How to find out object type - PLEASE HELP

      On 17 Apr 2004 11:06:41 -0700 in comp.lang.c++, cpptutor2000@ya hoo.com
      (Gandu) wrote,[color=blue]
      >Could some C++ guru please help me? Suppose I have a linked list, in which the
      > data stored is of type T (using templates) or void* (as in C). Is there any
      > way to find out the type of the object (other than type-casting) when the
      > object is extracted fronm the list? Thanks in advance for your help.[/color]

      If your pointer is void* that means you do not care about what type the
      object is, so the question is irrelevant. Cast not thy pointers into
      the void.

      If you are using a template on type T, there are several techniques,
      depending on just what you mean by "find out the type." What are you
      trying to accomplish? In general, it is probably better to write code
      where you do not do a lot of "finding out" of types.

      For example:

      typedef std::list<mycla ss> mylist;
      mylist some_list;
      mylist::value_t ype some_variable;

      Comment

      • Mark A. Gibbs

        #4
        Re: How to find out object type - PLEASE HELP


        Gandu wrote:
        [color=blue]
        > Could some C++ guru please help me? Suppose I have a linked list, in which the
        > data stored is of type T (using templates) or void* (as in C). Is there any
        > way to find out the type of the object (other than type-casting) when the
        > object is extracted fronm the list? Thanks in advance for your help.[/color]

        Don't use void*.

        If you define your linked list class like:

        template <typenameT>
        class LinkedList
        {
        public:
        typedef element_type T;
        // etc.
        };

        Then you can determine the element type like this:

        template <typename T>
        void foo(LinkedList< T> const&)
        {
        cout << typeid(LinkedLi st<T>::element_ type).name();
        }

        But I not sure that's what you are trying to do. Are you trying to make
        a list that can hold *anything*? That's not easy (and generally not
        wise). Or are you trying to make a list that can hold any object derived
        from a specific base class? That's easier. All you would have to do in
        that case is use typeid() on the extracted objects.

        It would help if you gave more information on what you are trying to do
        and why. It is not common to need to get the type of an object. There is
        probably a better solution that you're not seeing.

        mark

        Comment

        • Alf P. Steinbach

          #5
          Re: How to find out object type - PLEASE HELP

          * "Mark A. Gibbs" <x_gibbsmark@ro gers.com_x> schriebt:[color=blue]
          >
          > But I not sure that's what you are trying to do. Are you trying to make
          > a list that can hold *anything*? That's not easy[/color]

          Have never tried that in C++, but what about e.g.


          std::list<boost ::any> theList;


          assuming that works, isn't that easy?

          --
          A: Because it messes up the order in which people normally read text.
          Q: Why is top-posting such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Mark A. Gibbs

            #6
            Re: How to find out object type - PLEASE HELP


            Alf P. Steinbach wrote:
            [color=blue]
            > std::list<boost ::any> theList;
            >
            >
            > assuming that works, isn't that easy?[/color]

            Yes and no. First of all, Boost.Any does not hold *anything*. But if you
            are ok with the limitations on what it can hold, you're free to use it.

            Secondly, I thought I made it clear I was referring doing it yourself,
            but I can see that I didn't. Duplicating the capabilities of Boost.Any
            is no trivial task.

            mark

            Comment

            • Daniel T.

              #7
              Re: How to find out object type - PLEASE HELP

              cpptutor2000@ya hoo.com (Gandu) wrote:
              [color=blue]
              > Could some C++ guru please help me? Suppose I have a linked list, in which the
              > data stored is of type T (using templates) or void* (as in C). Is there any
              > way to find out the type of the object (other than type-casting) when the
              > object is extracted fronm the list? Thanks in advance for your help.[/color]

              How could you not know what the type of the contained objects are? You
              created the list!

              std::list<std:: string> string_list;

              I know the objects in the string_list are of type std::string. Maybe
              there is some miscommunicatio n here...

              Comment

              • Gandu

                #8
                Re: How to find out object type - PLEASE HELP

                Thank you all for your very useful suggestions. A colleague had suggested a
                generic container class be created that will hold "anything", so this question
                came up.


                "Mark A. Gibbs" <x_gibbsmark@ro gers.com_x> wrote in message news:<PIfgc.981 74$2oI1.15340@t wister01.bloor. is.net.cable.ro gers.com>...[color=blue]
                > Gandu wrote:
                >[color=green]
                > > Could some C++ guru please help me? Suppose I have a linked list, in which the
                > > data stored is of type T (using templates) or void* (as in C). Is there any
                > > way to find out the type of the object (other than type-casting) when the
                > > object is extracted fronm the list? Thanks in advance for your help.[/color]
                >
                > Don't use void*.
                >
                > If you define your linked list class like:
                >
                > template <typenameT>
                > class LinkedList
                > {
                > public:
                > typedef element_type T;
                > // etc.
                > };
                >
                > Then you can determine the element type like this:
                >
                > template <typename T>
                > void foo(LinkedList< T> const&)
                > {
                > cout << typeid(LinkedLi st<T>::element_ type).name();
                > }
                >
                > But I not sure that's what you are trying to do. Are you trying to make
                > a list that can hold *anything*? That's not easy (and generally not
                > wise). Or are you trying to make a list that can hold any object derived
                > from a specific base class? That's easier. All you would have to do in
                > that case is use typeid() on the extracted objects.
                >
                > It would help if you gave more information on what you are trying to do
                > and why. It is not common to need to get the type of an object. There is
                > probably a better solution that you're not seeing.
                >
                > mark[/color]

                Comment

                • Mark A. Gibbs

                  #9
                  Re: How to find out object type - PLEASE HELP


                  Gandu wrote:
                  [color=blue]
                  > Thank you all for your very useful suggestions. A colleague had suggested a
                  > generic container class be created that will hold "anything", so this question
                  > came up.[/color]

                  If I were you, I'd take your colleague to task on why this is necessary.
                  I can't see a reasonable argument in favour of doing that. I mean, what
                  possible use can there be for holding an integer, a string, a stream, a
                  student an a* path analyser with tactical weighting and a random number
                  generator in the same place?

                  mark

                  Comment

                  Working...