473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Oct 5 '06 #1
6 1360
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-lit....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-lit...alization.html

and check out Boost.Serialization:

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

Cheers! --M

Oct 5 '06 #2
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
Oct 5 '06 #3

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-lit....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-lit...alization.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.

Oct 5 '06 #4
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
Oct 5 '06 #5
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-lit....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-lit...alization.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

Oct 5 '06 #6
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,0red;

etc.

regards
Andy Little

Oct 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1308
by: Joseph Geretz | last post by:
Our company develops and markets a client/server application which is written in VB6 as a rich Win32 Client/Server application. For a variety of technological reasons we are looking to migrate...
6
1296
by: Kay | last post by:
Hello, My question isn't specific to .Net but as .Net is object oriented, I would be interested if someone could let me know if there is a standard way of implementing what I describe below in...
3
3037
by: pertheli | last post by:
Hello, I have a large array of pointer to some object. I have to run test such that every possible pair in the array is tested. eg. if A,B,C,D are items of the array, possible pairs are AB, AC,...
1
1863
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
21
4030
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
3
1410
by: Miguel | last post by:
Hi all friends: It's said that Sessions objects in ASP 3.0 with IIS 5.0 occupy certain memory of the machine which take to take care about use a lot of Sessions objects in the ASPs pages of the...
4
3303
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
10
1929
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
7
6409
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
13
2692
by: John Kraft | last post by:
Friends, I'm working on some crud stuff, and I was looking for opinions on the subject. Below, I have pasted some VERY simple sample code. Class2 is a "traditional" crud type object. In a...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.