casting from a string object (doing like Class.forName in java)

Discussion in 'C++' started by jean-francois, Jan 19, 2007.

  1. In java you can do it with
    Class.forName("myClass").newInstance();
    this will return you a new instance of the class myClass, provided that
    you defined it elsewhere. How can I get this in c++ (linux)?
    For exemple I'm fetching elements from a vector, they are all
    subclasses of a base class Field, but the data type within each
    subclass can vary according to a template that was used (exemple
    (SubField<int>, Subfield<long>,. Subfield<myclass>, etc..) The objects
    were stored/retrieved with the base class pointer Field since they are
    taken from a vector
    I can still define a common method "void * getData()" in the base class
    field but I need to know how to recast once I fetch the values from the
    vector if I'm doing some processing with the data.
    I'm at least able to store the data name in a member variable, i.e.

    SubField<T>: public Field
    <template typename T>
    setData( T data) {
    m_data = data;
    m_dataType = typeid(data).name();
    }
    But once I extract back this object from the vector the string
    m_dataType that I can fetch from the Field *object is useless unless I
    use a big switch ( I don't like it)
    So how can one instanciate an object or define a cast type with only a
    string descriptor?
    jean-francois, Jan 19, 2007
    #1
    1. Advertising

  2. jean-francois wrote:
    > In java you can do it with
    > Class.forName("myClass").newInstance();
    > this will return you a new instance of the class myClass, provided
    > that you defined it elsewhere. How can I get this in c++ (linux)?


    Yes, you can, if you implement it yourself. Basically, you need
    a bunch of functions (factories) which would create your objects,
    and then introduce a factory manager which will have every factory
    named. Then you can call your manager's 'forNameNewInstance' and
    get back a pointer (void*, most likely) to the newly created object.

    > For exemple I'm fetching elements from a vector, they are all
    > subclasses of a base class Field, but the data type within each
    > subclass can vary according to a template that was used (exemple
    > (SubField<int>, Subfield<long>,. Subfield<myclass>, etc..) The objects
    > were stored/retrieved with the base class pointer Field since they are
    > taken from a vector
    > I can still define a common method "void * getData()" in the base
    > class field but I need to know how to recast once I fetch the values
    > from the vector if I'm doing some processing with the data.
    > I'm at least able to store the data name in a member variable, i.e.
    >
    > SubField<T>: public Field
    > <template typename T>
    > setData( T data) {
    > m_data = data;
    > m_dataType = typeid(data).name();
    > }
    > But once I extract back this object from the vector the string
    > m_dataType that I can fetch from the Field *object is useless unless I
    > use a big switch ( I don't like it)
    > So how can one instanciate an object or define a cast type with only a
    > string descriptor?


    See above.

    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, Jan 19, 2007
    #2
    1. Advertising

  3. jean-francois

    I V Guest

    On Fri, 19 Jan 2007 09:36:17 -0800, jean-francois wrote:
    > For exemple I'm fetching elements from a vector, they are all
    > subclasses of a base class Field, but the data type within each
    > subclass can vary according to a template that was used (exemple
    > (SubField<int>, Subfield<long>,. Subfield<myclass>, etc..) The objects
    > were stored/retrieved with the base class pointer Field since they are
    > taken from a vector
    > I can still define a common method "void * getData()" in the base class
    > field but I need to know how to recast once I fetch the values from the
    > vector if I'm doing some processing with the data.


    You might find that Boost.Variant gives you a different (and possibly
    better) way to solve your problem. See
    http://www.boost.org/doc/html/variant.html
    I V, Jan 19, 2007
    #3
    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. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,727
    H.MuthuKumaraRajan
    Nov 7, 2003
  2. cyril
    Replies:
    2
    Views:
    3,857
    cyril
    Aug 25, 2004
  3. Madni
    Replies:
    1
    Views:
    5,878
  4. Alex Hunsley
    Replies:
    14
    Views:
    584
    Heiko Wundram
    Jul 19, 2004
  5. femto femto
    Replies:
    3
    Views:
    170
    Trans
    Aug 17, 2006
Loading...

Share This Page