Can I declare my Class at several blocks.

Discussion in 'C++' started by sandSpiderX, Jul 11, 2005.

  1. sandSpiderX

    sandSpiderX Guest

    Hi,

    I am a C++ SE. I was workingon a project, instantly I thought of trying
    to declare a class at several blocks,

    is it possible,

    like

    Class X
    { int i;
    };

    { ....}

    class X
    {
    float f;
    void f(double d){}
    };

    is it possible,
    is there some way I can achieve this,
    I am thinking of building a user defined data type at run time, by
    asking infromation from the user.

    Help
    sandX
     
    sandSpiderX, Jul 11, 2005
    #1
    1. Advertising

  2. Re: Can I declare my Class at several blocks.

    > I am a C++ SE. I was workingon a project, instantly I thought of trying
    > to declare a class at several blocks,
    > is it possible,


    No. Namespaces work that way, but not classes. Classes are compile-time
    creatures.


    Jonathan
     
    Jonathan Mcdougall, Jul 11, 2005
    #2
    1. Advertising

  3. sandSpiderX

    sandSpiderX Guest

    Re: Can I declare my Class at several blocks.

    :Jonathan

    Well, okey.
    So it means I cannot generate data types at compile time.
    Is there any language which supports this.

    sandX
     
    sandSpiderX, Jul 11, 2005
    #3
  4. Re: Can I declare my Class at several blocks.

    > Well, okey.
    > So it means I cannot generate data types at compile time.
    > Is there any language which supports this.


    Not that I know of. Java has an impressive support for reflection
    (knowing at run-time informations about a type), but cannot modify
    anything at run-time. In fact, compiled languages are, to the best of
    my knowledge, incapable of modifying types.

    You may look at scripting languages such as PHP, but even then I think
    it will be quite difficult.

    Perhaps if you enlighten us about your design, we may be of more help.


    Jonathan
     
    Jonathan Mcdougall, Jul 11, 2005
    #4
  5. sandSpiderX

    Jaspreet Guest

    Re: Can I declare my Class at several blocks.

    sandSpiderX wrote:
    > Hi,
    >
    > I am a C++ SE. I was workingon a project, instantly I thought of trying
    > to declare a class at several blocks,
    >
    > is it possible,
    >
    > like
    >
    > Class X
    > { int i;
    > };
    >
    > { ....}
    >
    > class X
    > {
    > float f;
    > void f(double d){}
    > };
    >
    > is it possible,
    > is there some way I can achieve this,
    > I am thinking of building a user defined data type at run time, by
    > asking infromation from the user.
    >
    > Help
    > sandX


    I was wondering if you could use templates to somehow solve the
    problem.

    template <typename T>
    class X
    {
    T a;
    }

    So, here T could be int/float/double/etc.
     
    Jaspreet, Jul 11, 2005
    #5
  6. sandSpiderX

    msalters Guest

    Re: Can I declare my Class at several blocks.

    sandSpiderX schreef:
    > :Jonathan
    >
    > Well, okey.
    > So it means I cannot generate data types at compile time.
    > Is there any language which supports this.


    It is possible. Read "Modern C++ Design", or see the Boost MPL
    libraries. At run-time things are slightly more difficult, but
    a std::map<std::string, boost::any> is often a 95% solution.

    Regards,
    Michiel Salters
     
    msalters, Jul 11, 2005
    #6
  7. sandSpiderX

    benben Guest

    "sandSpiderX" <> wrote in message
    news:...
    > Hi,
    >
    > I am a C++ SE. I was workingon a project, instantly I thought of trying
    > to declare a class at several blocks,
    >
    > is it possible,
    >
    > like
    >
    > Class X
    > { int i;
    > };
    >
    > { ....}
    >
    > class X
    > {
    > float f;
    > void f(double d){}
    > };
    >
    > is it possible,
    > is there some way I can achieve this,
    > I am thinking of building a user defined data type at run time, by
    > asking infromation from the user.
    >
    > Help
    > sandX
    >


    If you only want to pull a complete class definition apart in several
    blocks, I guess you can do it by multiple inheritance:

    class X;

    class X_block1
    {
    friend class X;
    //...
    };

    class X_block2
    {
    friend class X;
    // ...
    };

    //...

    class X: private X_block1, private X_block2 ...
    {
    // constructor, destructor, etc...
    };

    But if you want to change type definition at runtime, there's NO WAY you can
    do it. In fact, type definition is a concept only exists at compile time.

    ben
     
    benben, Jul 12, 2005
    #7
  8. sandSpiderX

    sandSpiderX Guest

    Re: Can I declare my Class at several blocks.

    I am inteding to build a game ....
    In this game, its an RPG, all humans are ientified by their true
    traits...

    So , I am building datatypes at runtime...
    Else, generalising , human traits is quiet huge and ineffecient..

    Its like, the player logs in, enters his traits, and start playing,
    I create a data type based on his info at run time and tell game's AI
    that hey here is another player, add and modify.

    This game idea , I find is too real.
    Help me with this

    sandX
     
    sandSpiderX, Jul 12, 2005
    #8
  9. Re: Can I declare my Class at several blocks.

    > I am inteding to build a game ....
    > In this game, its an RPG, all humans are ientified by their true
    > traits...


    Ok.

    > So , I am building datatypes at runtime...
    > Else, generalising , human traits is quiet huge and ineffecient..


    Humans may be caracterised by generic traits. How generic they are
    depends on the design. For example, you may have an 'intelligence'
    value, from 1 to 20, or different values for 'memory', 'imagination'
    and 'reason'. Or something like that.

    > Its like, the player logs in, enters his traits, and start playing,
    > I create a data type based on his info at run time and tell game's AI
    > that hey here is another player, add and modify.


    You don't need runtime type modifications for that, iiuc. What are the
    traits? D&D like, such as strength, intelligence and so on? In that
    case, put some ints in a class and here you are.

    You usually want to change the value, not the type of an object.
    Please, explain to us exactly, with a complete example, what you mean
    with "creating new data type based on his info".


    Jonathan
     
    Jonathan Mcdougall, Jul 12, 2005
    #9
  10. sandSpiderX

    benben Guest

    Re: Can I declare my Class at several blocks.

    > I am inteding to build a game ....
    > In this game, its an RPG, all humans are ientified by their true
    > traits...
    >
    > So , I am building datatypes at runtime...
    > Else, generalising , human traits is quiet huge and ineffecient..
    >
    > Its like, the player logs in, enters his traits, and start playing,
    > I create a data type based on his info at run time and tell game's AI
    > that hey here is another player, add and modify.
    >
    > This game idea , I find is too real.
    > Help me with this
    >
    > sandX
    >


    Sounds like you need some database support. Consider std::map<std::string,
    std::string> as a growable "generic" data type (in which you have to store
    everything in strings).

    ben
     
    benben, Jul 12, 2005
    #10
    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. Arjen
    Replies:
    3
    Views:
    471
    Scott Allen
    Feb 27, 2005
  2. John Black
    Replies:
    2
    Views:
    383
    jeffc
    Jun 15, 2004
  3. matt
    Replies:
    1
    Views:
    296
    George Ogata
    Aug 6, 2004
  4. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    199
    Max Lapshin
    Mar 16, 2007
  5. Steven Taylor
    Replies:
    9
    Views:
    280
    Brian Candler
    Apr 27, 2009
Loading...

Share This Page