Question about objects in objects.

Discussion in 'C++' started by JoeC, Oct 5, 2006.

  1. JoeC

    JoeC Guest

    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(int 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.
    JoeC, Oct 5, 2006
    #1
    1. Advertising

  2. JoeC

    mlimber Guest

    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(int 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:

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6

    > 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:

    http://www.parashift.com/c -faq-lite/serialization.html

    and check out Boost.Serialization:

    http://boost.org/libs/serialization/doc/index.html

    Cheers! --M
    mlimber, Oct 5, 2006
    #2
    1. Advertising

  3. 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(int 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 "initialiser 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
    Victor Bazarov, Oct 5, 2006
    #3
  4. JoeC

    JoeC Guest

    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(int 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:
    >
    > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6
    >
    > > 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:
    >
    > http://www.parashift.com/c -faq-lite/serialization.html
    >
    > and check out Boost.Serialization:
    >
    > http://boost.org/libs/serialization/doc/index.html
    >
    > 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.
    JoeC, Oct 5, 2006
    #4
  5. 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
    Victor Bazarov, Oct 5, 2006
    #5
  6. JoeC

    mlimber Guest

    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(int 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:
    > >
    > > http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6
    > >
    > > > 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:
    > >
    > > http://www.parashift.com/c -faq-lite/serialization.html
    > >
    > > and check out Boost.Serialization:
    > >
    > > http://boost.org/libs/serialization/doc/index.html
    > >
    > > 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
    mlimber, Oct 5, 2006
    #6
  7. JoeC

    kwikius Guest

    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,0> red;

    etc.

    regards
    Andy Little
    kwikius, Oct 5, 2006
    #7
    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. bigbinc
    Replies:
    3
    Views:
    388
    Michael Borgwardt
    Nov 18, 2003
  2. Simon Elliott

    Inheritance of objects within objects

    Simon Elliott, Dec 10, 2004, in forum: C++
    Replies:
    2
    Views:
    323
    Simon Elliott
    Dec 10, 2004
  3. =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=

    form objects into class objects

    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    424
    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=
    Jul 6, 2006
  4. Replies:
    4
    Views:
    561
    Joseph Kesselman
    Aug 10, 2006
  5. 7stud
    Replies:
    11
    Views:
    673
    Dennis Lee Bieber
    Mar 20, 2007
Loading...

Share This Page