Tips on Creating Objects

M

Michael DOUBEZ

JoeC a écrit :
I read through some of those articles you suggested. They were
interesting but I have some questions. How small should an object
be. From some of the examples it is almost to each variable should be
a class. I posted some of my work and I was hoping to get some ideas
on how I could break my classes down a bit. I have some ideas but I
was hoping to get a second opinion.

Grizlyk answered it in his first post: design patterns is what you want.
Depending on the tradeoff you have, one pattern or another is adapted to
your program.

As exemples, the flyweight pattern may help you manage a lot of units
with small memory usage, the decorator pattern may help you modify your
units transparently at runtime, the strategy pattern can help you define
at runtime various IA for units ...

all those designs will influence the way of partitionning and reusing
your classes.
what is wrong with a constructor ? obj::eek:bj(int, int, char); What is
the advantage of creating structures obj::eek:bj(strc&);

nothing wrong, no advantage.
obj::eek:bj(int, int, char) allows you to set default values for some
parameters.
I will try to begin re-writing my classes and breaking them down to
smaller parts. I don't have that much to go on to help me with my
design. I think graphical, movable, attackable, defndable.

Is it ok to have a class:

class unit : public movable, attackable, graphical, ..... To build a
class? If you look at my objects they are pretty big and do quite a
bit. They move, fight, display, become disbersed, have colors and
graphics.

I cannot help you with game design but your could have a look at the
decorator pattern and flyweight pattern I mentionned.

Michael
 
R

Richard Herring

Richard said:
You have said, that the code

Tlandscape &meadow= *new Tlandscape;

meadow.has(new Tgrass + new Tpine(3)+ new Tlake );
enum{ grass=0, pines, lake};
meadow[lake].has( new Tfish(100) );

Tperson &worker=*new Tworker;

worker.making(meadow.east, new Tditch);
const uint ditch=lake+1;

does not looks like C++ for you. I have posted the complete compileable
example just to prove that it is pure C++ code.

So what does your compiler say when you try to sum three pointers?

copy&paste to your compiler the example in message above

What, this "complete compileable example"? I pasted it inside a function
thus:
==========================================
int main()
{
Tlandscape &meadow= *new Tlandscape;

meadow.has(new Tgrass + new Tpine(3)+ new Tlake );
enum{ grass=0, pines, lake};
meadow[lake].has( new Tfish(100) );

Tperson &worker=*new Tworker;

worker.making(meadow.east, new Tditch);
const uint ditch=lake+1;

return 0;
}
=========================================
and the compiler said:

=========================================================================
======
d:\users\rnh\projects\temp\temp\temp\temp.cpp(4) : error C2065:
'Tlandscape' : undeclared identifier
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(4) : error C2065:
'meadow' : undeclared identifier
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(4) : error C2061: syntax
error : identifier 'Tlandscape'
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(6) : error C2228: left
of '.has' must have class/struct/union
1> type is ''unknown-type''
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(6) : error C2061: syntax
error : identifier 'Tgrass'
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(6) : error C2061: syntax
error : identifier 'Tlake'
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(8) : error C2228: left
of '.has' must have class/struct/union
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(8) : error C2061: syntax
error : identifier 'Tfish'
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(10) : error C2065:
'Tperson' : undeclared identifier
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(10) : error C2065:
'worker' : undeclared identifier
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(10) : error C2061:
syntax error : identifier 'Tworker'
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(12) : error C2228: left
of '.making' must have class/struct/union
1> type is ''unknown-type''
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(12) : error C2228: left
of '.east' must have class/struct/union
1> type is ''unknown-type''
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(12) : error C2061:
syntax error : identifier 'Tditch'
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(13) : error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(13) : error C2146:
syntax error : missing ';' before identifier 'ditch'
1>d:\users\rnh\projects\temp\temp\temp\temp.cpp(13) : error C2065:
'ditch' : undeclared identifier
=========================================================================
======

Is that really what you meant by "complete compileable example"?
 
J

JoeC

JoeC a écrit :



Grizlyk answered it in his first post: design patterns is what you want.
Depending on the tradeoff you have, one pattern or another is adapted to
your program.

As exemples, the flyweight pattern may help you manage a lot of units
with small memory usage, the decorator pattern may help you modify your
units transparently at runtime, the strategy pattern can help you define
at runtime various IA for units ...

all those designs will influence the way of partitionning and reusing
your classes.




nothing wrong, no advantage.
obj::eek:bj(int, int, char) allows you to set default values for some
parameters.






I cannot help you with game design but your could have a look at the
decorator pattern and flyweight pattern I mentionned.

Michael

OK fair enough. I am not trying to get help on my game just
suggestions on how I can do things differently. I will have to go and
read more on designing objects. I think jumping into rewriting my
game with a new design scheme is too ambitious, I thin I will have to
start with smaller programs to test different ways of writing
objects.

I will see about more studying. I have a pdf of design patterns and I
will print out the book and do my best to understand the concepts so I
can write better programs. I know how to create and use objects, I
know how do do dynamic binding so that objects can be more flexable.

My problem is that I am not sure how to efectivly build larger objects
from smaller ones. ISA then I inherit if is HASA then it is a
member. Looking at some of the examples it gets confusing, I am used
to more straight forward code. I will see what I can do to try
improve my programming.
 
G

Grizlyk

Richard said:
You have said, that the code

Tlandscape &meadow= *new Tlandscape;

meadow.has(new Tgrass + new Tpine(3)+ new Tlake );
enum{ grass=0, pines, lake};
meadow[lake].has( new Tfish(100) );

Tperson &worker=*new Tworker;

worker.making(meadow.east, new Tditch);
const uint ditch=lake+1;

does not looks like C++ for you. I have posted the complete compileable
example just to prove that it is pure C++ code.

So what does your compiler say when you try to sum three pointers?

copy&paste to your compiler the example in message above

What, this "complete compileable example"? I pasted it inside a function
thus:
==========================================
int main()
{
Tlandscape &meadow= *new Tlandscape;

meadow.has(new Tgrass + new Tpine(3)+ new Tlake );
enum{ grass=0, pines, lake};
meadow[lake].has( new Tfish(100) );

Tperson &worker=*new Tworker;

worker.making(meadow.east, new Tditch);
const uint ditch=lake+1;

return 0;
}
=========================================
and the compiler said:
copy&paste to your compiler the example in message above

in _neighbouring_ thread message above - search nearest messages in the
thread for complete example.
 
G

Grizlyk

JoeC said:
I read through some of those articles you suggested. They were
interesting but I have some questions. How small should an object
be.

Read the article (it seems to me i have suggested only one) befor questions.
 
R

Richard Herring

Richard said:
You have said, that the code

Tlandscape &meadow= *new Tlandscape;

meadow.has(new Tgrass + new Tpine(3)+ new Tlake );
enum{ grass=0, pines, lake};
meadow[lake].has( new Tfish(100) );

Tperson &worker=*new Tworker;

worker.making(meadow.east, new Tditch);
const uint ditch=lake+1;

does not looks like C++ for you. I have posted the complete compileable
example just to prove that it is pure C++ code.

So what does your compiler say when you try to sum three pointers?

copy&paste to your compiler the example in message above

What, this "complete compileable example"? I pasted it inside a function
thus:
==========================================
int main()
{
Tlandscape &meadow= *new Tlandscape;

meadow.has();
enum{ grass=0, pines, lake};
meadow[lake].has( new Tfish(100) );

Tperson &worker=*new Tworker;

worker.making(meadow.east, new Tditch);
const uint ditch=lake+1;

return 0;
}
=========================================
and the compiler said:
copy&paste to your compiler the example in message above

in _neighbouring_ thread message above - search nearest messages in the
thread for complete example.

No longer on this server, and you can't assume that anyone else reading
your non-C++ snippet will have access to it. Whether or not you posted
correct code elsewhere, you don't help anybody by posting fragments that
contain code that is not valid C++, such as this:

(new Tgrass + new Tpine(3)+ new Tlake)

The last time I looked at the standard the result of a new-expression
was a pointer (5.3.4) and if one operand of + was a pointer, the other
had to be of integral or enumeration type (5.7).
 
R

Richard Herring

G

Grizlyk

Richard said:
Not everyone has web access, and why should they bother anyway? My point
is not about the postings I can't see, but the ones I can, which contained
incorrect information. If you want to make a point, _you_ should post
complete correct code, not misleading fragments. You can't expect your
readers to go looking for posts they don't even know exist.

For the first, I must do for you nothing, for the second, I can post to you
copy of already posted message here by email, have you email and will you be
happy after?
 
R

Richard Herring

For the first, I must do for you nothing, for the second, I can post to you
copy of already posted message here by email, have you email and will you be
happy after?
This isn't a private dialogue. If you are going to post anything, post
it here, not by private email. That way _everyone_ who's interested can
see what we're talking about.
 
G

Grizlyk

Richard said:
This isn't a private dialogue. If you are going to post anything, post it
here, not by private email. That way _everyone_ who's interested can see
what we're talking about.

I take it as you have refused to get already posted message.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top