Question about objects in objects.

J

JoeC

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.
 
M

mlimber

JoeC said:
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
 
V

Victor Bazarov

JoeC said:
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
 
J

JoeC

mlimber said:

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.
 
V

Victor Bazarov

JoeC said:
[..]
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
 
M

mlimber

JoeC said:
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
 
K

kwikius

Victor said:
JoeC said:
[..]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top