Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » board, pawn and classes

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
board, pawn and classes
LennyLen
Member #5,313
December 2004
avatar

Quote:

And why my destructor isn't okey ?

That's only a declaration for your destructor. You either need to define the function elsewhere, or change the line to this:
~player() {}

As for X-G's comments, while he may be abrupt, he is correct. You really do need to get a better understanding of what you are doing. If you're learning from a book, and it has exercises, do them.

He is also correct in that you have not listened to advice that will make your code more readable to others.

kosmitek
Member #8,151
December 2006

I know I must read good book C++ and I will change my code according to your advices but later.

~player() {} - now I have declaration and definition of destructor in the class but I have one more question - I have also something wrong in activation of destructor in my function (second green text near the end of my code):
//game.~player(); // activation destructor - something is wrong
- what is wrong ?

PS. Thanks LennyLen.

Steve Terry
Member #1,989
March 2002
avatar

DON'T CALL THE FRIGGIN DESTRUCTOR MANUALLY... there is a construct called "delete" use it.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

kosmitek
Member #8,151
December 2006

delete game - it is bad and I think that we use "delete" when we allocate memory dynamically........

Steve Terry
Member #1,989
March 2002
avatar

We are here to help you, we keep giving you the correct advice and yet you still keep to your own predisposed ideas about C and C++, I try to help, do what you will.

Ok if you declare your class as Game game(); then you won't have to call delete, if you delcare it as Game *game = new Game(); then you have to use delete. In your case since you are not using new then you won't have to worry about calling the destructor at all, that will be done automatically once the class goes out of scope.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

TeamTerradactyl
Member #7,733
September 2006
avatar

kosmitek:

If at all possible, I would suggest using the code I uploaded here for you earlier. I'm not able to easily read your code, and I'm not entirely sure what's going on -- especially since I don't think you need things like:
int tab2[10]={0,1,2,3,4,5,6,7,8,9};
If you wanted an index of 0..4, and 5..9, just use literal numbers and not an array that point to "0..4, 5..9".

That being said, Steve Terry was correct: You never call the destructor itself:
game.~player();

You would do one of two things:

1) Nothing. When you hit the closing "}" for the main() loop, the object's "game.~player()" is called automatically. (This is nice, because you NEVER have to clean up your own code... it is all done automatically for you! ;D)
2) If you have an array of these, and you did something like this:

  player *thePlayer;
  thePlayer = new player(99, 99, 99, 99);

  ...

  delete thePlayer; // <-- You want to call DELETE, which calls the "~player()" destructor for you

Your "player" class IS correctly using classes, though: good job.

The only thing I would like to suggest, coming from a programmer's standpoint: It would be a little better if you were to use header files and keep your class declaration there, and then implement your code in a .CPP file. By doing this, it makes your code portable. If you don't do this, and someday you (or someone else) ever wants to use your class again, they're stuck with YOUR code... they can't simply change the parts they want (like, instead of setting money = a; health = b;, they wanted to use money = b; health = a;, etc.).

Also, with the code I posted for you earlier, the "goto" was removed: this was necessary, because otherwise, your program took up 100% of my CPU and was very "jerky". Having it in a function like I provided allowed the program to take up only 1%, and there was no "jerkiness".

You could VERY easily put your "player" class into the code I provided: the way that you rewrote this code (above) looks like you implemented "player" very smoothly, and it shouldn't be a problem.

<knitpick>Also, your code is harder to read because it's not indented consistently. You may want to give it a pass through either GNU indent or, my preference, aStyle (which is better for indenting/formatting C++).</knitpick>

Indeterminatus
Member #737
November 2000
avatar

<knitpick>

Quote:

You never call the destructor itself

...unless using placement new.
</knitpick>

However, as willing to help as I am, I'm with X-G on that one. kosmitek: I highly suggest you stop here, restart and progress slowly with the guidance of a (few) book(s). The bare amount of new information for you is plainly overwhelming and, if anything, almost certainly only confuses you.

As previously said, OOP is not throwing a few keywords like "class" in, it's a way of thinking. After having grasped the concepts, you can go on and make use of the syntactic sugar that C++ provides over C.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

kosmitek
Member #8,151
December 2006

Steve Terry and TeamTerradactyl thanks for your advice - so I won't declare destructor.
Only one question - now I have 1 object: player game(99, 99, 99, 99); and 4 components private:

1class player
2{
3 private:
4 int money;
5 int health;
6 int weapon;
7 int power;
8
9
10 public:
11 player(int a, int b, int c, int d)
12 {
13 money=a;
14 health=b;
15 weapon=c;
16 power=d;
17 }
18
19 int change_money(int x)
20 {
21 money=money+x;
22 return money;
23 }
24
25 int change_health(int x)
26 {
27 health=health+x;
28 return health;
29 }
30
31
32 int change_weapon(int x)
33 {
34 weapon=weapon+x;
35 return weapon;
36 }
37
38
39 int change_power(int x)
40 {
41 power=power+x;
42 return power;
43 }

Mayby better solution will be 4 objects

 player a(99);
player b(99);
player c(99);
player d(99);

and 1 component private:

1class player
2{
3 private:
4 int something;
5
6
7
8 public:
9 player(int x)
10 {
11 something=x;
12
13 }
14
15 int change_something(int y)
16 {
17 something=something+y;
18 return something;
19 }

Which solution is more OOP ??

PS. TeamTerradactyl I don't understand all your sentences so tomorrow I will use english dictionary :P;D

LennyLen
Member #5,313
December 2004
avatar

The second example defeats the entire purpose of data structures (both in C and C++), which is to keep related data together.

Instead of just having one player object, which contains everything you need in one place, you now have four player objects, each of which is storing a something, that you have to keep track of yourself.

Neither example is inherently more OOP, but the second one is just wrong.

X-G
Member #856
December 2000
avatar

Quote:

<knitpick>

... nitpick. Oh, the delicious irony!

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

TeamTerradactyl
Member #7,733
September 2006
avatar

<nitpick>

X-G said:

Quote:

<knitpick>

... Oh, the delicious irony!

</nitpick>

X-G: Sorry; I'm too used to sewing and picking out "knitting" stitches (hence, "knit" and "pick"). :-/;)

kosmitek
Member #8,151
December 2006

Thank you LennyLen.

The most help me: TeamTerradactyl. Also help me: LennyLen and Steve Terry.

Can I give points for help me on this forum ?

LennyLen
Member #5,313
December 2004
avatar

Quote:

Can I give points for help me on this forum ?

Yes, but only if you classified the thread as "A question or problem with a specific answer" when you created it.

X-G
Member #856
December 2000
avatar

Just because you don't like the answer doesn't mean it's not the correct/most helpful one. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Albin Engström
Member #8,110
December 2006
avatar

X-G: "Just because you don't like the answer doesn't mean it's not the correct/most helpful one."

Indeed, Although i must say that i know the feeling you get when people nag your ideas, i have acted bullheaded many times on this forum, but as i'm not an experienced programmer i don't ignore what people say and i certainly don't get angry when people say i'm way of the road. You should listen more to what people say kosmitek, information, suggestions and criticism is never a bad thing.

 1   2   3 


Go to: