Re: Construction of classes at runtime

Discussion in 'C++' started by James Kanze, Oct 8, 2008.

  1. James Kanze

    James Kanze Guest

    On Aug 20, 11:47 am, Ian Collins <> wrote:
    > Sami wrote:
    > You can't create a class 'on the fly' from its name in C++,
    > you have to know the classes at compile time.


    That's true for pretty much every language, isn't it. It's not
    sufficient to just have a name; you need more information from
    somewhere. And you can make things pretty dynamic in C++; I
    have one case where I look for a dynamically linked object for
    the class if it isn't already loaded.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Oct 8, 2008
    #1
    1. Advertising

  2. James Kanze

    Ian Collins Guest

    James Kanze wrote:
    > On Aug 20, 11:47 am, Ian Collins <> wrote:
    >> Sami wrote:
    >> You can't create a class 'on the fly' from its name in C++,
    >> you have to know the classes at compile time.

    >
    > That's true for pretty much every language, isn't it. It's not
    > sufficient to just have a name; you need more information from
    > somewhere. And you can make things pretty dynamic in C++; I
    > have one case where I look for a dynamically linked object for
    > the class if it isn't already loaded.
    >

    True, but a compiled language will never match the runtime flexibility
    of an interpreted one. For example in PHP to process an RPC request I
    can write

    call_user_func_array( array($object,$method), $parameters );

    Where $object is the name of a class, $method is the method to call and
    $parameters an array of parameters. The interpreter will find the
    class, construct it and call the method. I could write the same
    function in C++, but the tables of classes and methods would have to
    explicitly constructed.

    --
    Ian Collins.
    Ian Collins, Oct 9, 2008
    #2
    1. Advertising

  3. James Kanze

    James Kanze Guest

    On Oct 9, 3:59 am, Ian Collins <> wrote:
    > James Kanze wrote:
    > > On Aug 20, 11:47 am, Ian Collins <> wrote:
    > >> Sami wrote:
    > >> You can't create a class 'on the fly' from its name in C++,
    > >> you have to know the classes at compile time.


    > > That's true for pretty much every language, isn't it. It's not
    > > sufficient to just have a name; you need more information from
    > > somewhere. And you can make things pretty dynamic in C++; I
    > > have one case where I look for a dynamically linked object for
    > > the class if it isn't already loaded.


    > True, but a compiled language will never match the runtime
    > flexibility of an interpreted one.


    Yes. There are, in fact, several different levels of
    flexibility possible (with, generally, more flexibility implying
    less robustness). In totally dynamic languages, with fully
    dynamic typing, a "struct" or a "class" is really nothing more
    than an associative array; an array whose elements are indexed
    by the name of the field. In such cases, you can dynamically
    read a set of attribute value pairs, and use the results as a
    struct. In most compiled languages, and some interpreted ones
    as well, I think, the program can only deal with structs known
    to it. With dynamic linking, however, it is possible to make
    the set of such structs open, to add to it at runtime.

    > For example in PHP to process an RPC request I can write


    > call_user_func_array( array($object,$method), $parameters );


    > Where $object is the name of a class, $method is the method to
    > call and $parameters an array of parameters. The interpreter
    > will find the class, construct it and call the method. I
    > could write the same function in C++, but the tables of
    > classes and methods would have to explicitly constructed.


    Or automatically generated by some other program:). I'm not
    familiar with PHP, but what you are describing doesn't sound too
    different from Java's java.lang.Class.forName( "className" )
    ..newInstance(). As I said, I've actually implemented this once
    in C++, with the programming looking for a corresponding
    ..so/.dll in a path if the desired class wasn't already present.
    (It's interesting to note that when I did more or less the same
    thing in Java, it was almost as many lines of code. Since, of
    course, not just any class would do; the class had to meet
    certain requirements, and I needed code to test those. In C++,
    the .so/.dll wouldn't link if the class didn't meet my
    requirements.)

    Implementing total flexibility is also more or less possible;
    your actual instance is basically an std::map< std::string,
    boost::any >. Of course, total flexibility often results in
    total chaos, but like everything else, with a good enough
    process and some programmer discipline, it can be made to work.
    If you really, really need it.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Oct 9, 2008
    #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. Steven T. Hatton

    Recursive construction of derived classes.

    Steven T. Hatton, May 15, 2005, in forum: C++
    Replies:
    1
    Views:
    427
    Steven T. Hatton
    May 15, 2005
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,071
    Gordon Beaton
    May 22, 2006
  3. Ook
    Replies:
    10
    Views:
    536
  4. coltrane
    Replies:
    5
    Views:
    463
    James Kanze
    Aug 25, 2010
  5. Evert Wiesenekker
    Replies:
    3
    Views:
    96
    Evert Wiesenekker
    Mar 26, 2006
Loading...

Share This Page