Question about objects in objects.

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

    #1

    Question about objects in objects.

    I have been writing a game and some of my objects have objecs as
    members of that I have a graphic with a color or unit with a coord
    structure.

    If I have a:
    class unit{
    color cl;
    int attackFactor;
    .....
    .....

    When I create the object, I create an empty color but I have a color
    constructor:
    color::color(in t r, int g, int b);

    I want to create the unit
    unit::unit(int af, int r, int g, int b);
    attackFactor =af;
    color(r,g,b);

    My unit objects will have quite a bit of data so I want to load all the
    information with istream from a text file. How can I use constructor
    for the member objects of my unit object or any other object that is a
    member of another object.

  • mlimber

    #2
    Re: Question about objects in objects.

    JoeC wrote:
    I have been writing a game and some of my objects have objecs as
    members of that I have a graphic with a color or unit with a coord
    structure.
    >
    If I have a:
    class unit{
    color cl;
    int attackFactor;
    ....
    ....
    >
    When I create the object, I create an empty color but I have a color
    constructor:
    color::color(in t r, int g, int b);
    >
    I want to create the unit
    unit::unit(int af, int r, int g, int b);
    attackFactor =af;
    color(r,g,b);
    Use an initialization list:


    My unit objects will have quite a bit of data so I want to load all the
    information with istream from a text file. How can I use constructor
    for the member objects of my unit object or any other object that is a
    member of another object.
    See these FAQs:



    and check out Boost.Serializa tion:



    Cheers! --M

    Comment

    • Victor Bazarov

      #3
      Re: Question about objects in objects.

      JoeC wrote:
      I have been writing a game and some of my objects have objecs as
      members of that I have a graphic with a color or unit with a coord
      structure.
      >
      If I have a:
      class unit{
      color cl;
      int attackFactor;
      ....
      ....
      >
      When I create the object, I create an empty color but I have a color
      constructor:
      color::color(in t r, int g, int b);
      >
      I want to create the unit
      unit::unit(int af, int r, int g, int b);
      attackFactor =af;
      color(r,g,b);
      >
      My unit objects will have quite a bit of data so I want to load all
      the information with istream from a text file. How can I use
      constructor for the member objects of my unit object or any other
      object that is a member of another object.
      Read about "initialise r list" in your favourite C++ book.

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • JoeC

        #4
        Re: Question about objects in objects.


        mlimber wrote:
        JoeC wrote:
        I have been writing a game and some of my objects have objecs as
        members of that I have a graphic with a color or unit with a coord
        structure.

        If I have a:
        class unit{
        color cl;
        int attackFactor;
        ....
        ....

        When I create the object, I create an empty color but I have a color
        constructor:
        color::color(in t r, int g, int b);

        I want to create the unit
        unit::unit(int af, int r, int g, int b);
        attackFactor =af;
        col
        or(r,g,b);
        >
        Use an initialization list:
        >

        >
        My unit objects will have quite a bit of data so I want to load all the
        information with istream from a text file. How can I use constructor
        for the member objects of my unit object or any other object that is a
        member of another object.
        >
        See these FAQs:
        >

        >
        and check out Boost.Serializa tion:
        >

        >
        Cheers! --M
        That is unit::unit(int x, int y) : xloc(x), yloc(y)?
        So I can:
        unit::unit(int x, int y, int z) : xloc(x), yloc(y), color cl(z);
        Have to look up the correct syntax.

        for example?

        I thought I would have to:

        class unit{
        color * cl;
        int attackFactor;
        ....
        unit::unit(int af, int r, int g, int b);
        attackFactor =af;
        cl = new color(r,g,b);

        and use a bunch of pointers.

        I will have the C++ programming language I hope I can find somthing
        useful there.

        Comment

        • Victor Bazarov

          #5
          Re: Question about objects in objects.

          JoeC wrote:
          [..]
          I thought I would have to:
          >
          class unit{
          color * cl;
          int attackFactor;
          ...
          unit::unit(int af, int r, int g, int b);
          Did you mean to use '{' instead of ';'?
          attackFactor =af;
          cl = new color(r,g,b);
          >
          and use a bunch of pointers.
          I don't see what "a bunch of pointers" have to do with it, but I am
          not as bright as I used to be...

          Again, instead of using assignments, use initialisation list. Read
          the FAQ, it's all explained there.
          >
          I will have the C++ programming language I hope I can find somthing
          useful there.
          If you mean the Bjarne Stroustrup's book, you definitely can.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • mlimber

            #6
            Re: Question about objects in objects.

            JoeC wrote:
            mlimber wrote:
            JoeC wrote:
            I have been writing a game and some of my objects have objecs as
            members of that I have a graphic with a color or unit with a coord
            structure.
            >
            If I have a:
            class unit{
            color cl;
            int attackFactor;
            ....
            ....
            >
            When I create the object, I create an empty color but I have a color
            constructor:
            color::color(in t r, int g, int b);
            >
            I want to create the unit
            unit::unit(int af, int r, int g, int b);
            attackFactor =af;
            color(r,g,b);
            Use an initialization list:


            My unit objects will have quite a bit of data so I want to load all the
            information with istream from a text file. How can I use constructor
            for the member objects of my unit object or any other object that is a
            member of another object.
            See these FAQs:



            and check out Boost.Serializa tion:



            Cheers! --M
            >
            That is unit::unit(int x, int y) : xloc(x), yloc(y)?
            So I can:
            unit::unit(int x, int y, int z) : xloc(x), yloc(y), color cl(z);
            Have to look up the correct syntax.
            Which you can find in the aforementioned FAQ, also.
            for example?
            Again, see the FAQ.
            I thought I would have to:
            >
            class unit{
            color * cl;
            int attackFactor;
            ...
            unit::unit(int af, int r, int g, int b);
            attackFactor =af;
            cl = new color(r,g,b);
            >
            and use a bunch of pointers.
            Nope. See the FAQ.
            I will have the C++ programming language I hope I can find somthing
            useful there.
            I presume you mean the book by Stroustrup. Hopefully your next
            investment will be in a proofreader. It's hard to follow what you're
            typing.

            Cheers! --M

            Comment

            • kwikius

              #7
              Re: Question about objects in objects.

              Victor Bazarov wrote:
              JoeC wrote:
              [..]
              I thought I would have to:

              class unit{
              color * cl;
              int attackFactor;
              ...
              unit::unit(int af, int r, int g, int b);
              >
              Did you mean to use '{' instead of ';'?
              >
              attackFactor =af;
              cl = new color(r,g,b);

              and use a bunch of pointers.
              >
              I don't see what "a bunch of pointers" have to do with it, but I am
              not as bright as I used to be...
              >
              Again, instead of using assignments, use initialisation list. Read
              the FAQ, it's all explained there.
              If your colour is fixed you could also use a template:

              template<int R, int G, int B>
              struct color{

              static const int red = R;
              static const int green = G;
              ststaic const int Blue = B;

              };

              typedef color<255,0,0re d;

              etc.

              regards
              Andy Little

              Comment

              Working...