Item class
Moderator: Coders of Rage
- Bullet Pulse
- Chaos Rift Cool Newbie
- Posts: 89
- Joined: Sun Feb 21, 2010 6:25 pm
Item class
So, I was thinking of ways to implement an Item class in my project.
Each item object will belong to a group (hp, money, etc), have a modifier value (5, 10, 15) and have a name (goldBar, coin, etc).
The group, modifier value, and name affect how the item is used in the use method.
For example, the Player object will be modified in Z way by Y much.
Method 1 : Item class will contain a static pointer to a Player object,
which can be modified directly though mutator methods called on the player object by item objects.
Method 2: The Player class will have its own method for using Item objects.
How do you think I should do it?
You can suggest a different method if you'd like.
Each item object will belong to a group (hp, money, etc), have a modifier value (5, 10, 15) and have a name (goldBar, coin, etc).
The group, modifier value, and name affect how the item is used in the use method.
For example, the Player object will be modified in Z way by Y much.
Method 1 : Item class will contain a static pointer to a Player object,
which can be modified directly though mutator methods called on the player object by item objects.
Method 2: The Player class will have its own method for using Item objects.
How do you think I should do it?
You can suggest a different method if you'd like.
Last edited by Bullet Pulse on Mon May 03, 2010 8:13 pm, edited 2 times in total.
-
- Chaos Rift Junior
- Posts: 345
- Joined: Tue Jan 12, 2010 7:23 pm
- Favorite Gaming Platforms: PC - Windows 7
- Programming Language of Choice: c++;haxe
- Contact:
Re: How to prevent infinite inclusion?
That may just work? Have you actually tried it?
If not, there may be a #pragma but I wouldn't count on it. You'll have to redo your classes to prevent the recursion.
If not, there may be a #pragma but I wouldn't count on it. You'll have to redo your classes to prevent the recursion.
- Bullet Pulse
- Chaos Rift Cool Newbie
- Posts: 89
- Joined: Sun Feb 21, 2010 6:25 pm
Re: How to prevent infinite inclusion?
Sorry, I changed the topic to something that will actually prevent the infinite inclusion. So please take a look at itLive-Dimension wrote:That may just work? Have you actually tried it?
If not, there may be a #pragma but I wouldn't count on it. You'll have to redo your classes to prevent the recursion.
And yes have tried it.
Re: Item class
try this, create a header called something like, dependancies.h
then in this, declare all class names first, then include all the headers next.
now, just include dependancies.h in all the other headers.
this should solve your problem.
also, you need header guards in all your headers.
should look something like this.
then in this, declare all class names first, then include all the headers next.
now, just include dependancies.h in all the other headers.
this should solve your problem.
also, you need header guards in all your headers.
should look something like this.
Code: Select all
//dependancies.h
#ifndef _dependancies_h
#define _dependancies_h
class jew;
class shoe;
class kangaroo;
#include "jew.h"
#include "shoe.h"
#include "kangaroo.h"
#endif
Last edited by avansc on Mon May 03, 2010 9:02 pm, edited 1 time in total.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Item class
The way I see it, it would probably be best to have an item function taking a parameter of a player class.Bullet Pulse wrote:So, I was thinking of ways to implement an Item class in my project.
Each item object will belong to a group (hp, money, etc), have a modifier value (5, 10, 15) and have a name (goldBar, coin, etc).
The group, modifier value, and name affect how the item is used in the use method.
For example, the Player object will be modified in Z way by Y much.
Method 1 : Item class will contain a static pointer to a Player object,
which can be modified directly though mutator methods called on the player object by item objects.
Method 2: The Player class will have its own method for using Item objects.
How do you think I should do it?
You can suggest a different method if you'd like.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: Item class
that's what I do, works wonders for the life of a programmer :Davansc wrote:try this, create a header called something like, dependancies.h
then in this, declare all class names first, then include all the headers next.
now, just include dependancies.h in all the other headers.
this should solve your problem.
should look something like this.
Code: Select all
//dependancies.h #ifndef _dependancies_h #define _dependancies_h class jew; class shoe; class kangaroo; #include "jew.h" #include "shoe.h" #include "kangaroo.h" #endif
well actually I don't put classes in there
Re: Item class
eatcomix, you should, because if you dont declare the classes, you can run into issues where the header guards cause issues
i took the time to illustrate the issue
deps.h
a.h
b.h
if you compile this you'll see it bitches at you that b is not declared.,
if you uncomment the class declarations it will compile fine.
anyways, just something to take note of.
i took the time to illustrate the issue
deps.h
Code: Select all
/*
* deps.h
* header
*
* Created by Andre van-Schalkwyk on 5/3/10.
* Copyright 2010 N/A. All rights reserved.
*
*/
#ifndef _deps_h
#define _deps_h
/*class a;
class b;*/
#include "a.h"
#include "b.h"
#endif
Code: Select all
/*
* a.h
* header
*
* Created by Andre van-Schalkwyk on 5/3/10.
* Copyright 2010 N/A. All rights reserved.
*
*/
#ifndef _a_h
#define _a_h
#include "deps.h"
class a
{
public:
private:
b *bclass;
};
#endif
Code: Select all
/*
* b.h
* header
*
* Created by Andre van-Schalkwyk on 5/3/10.
* Copyright 2010 N/A. All rights reserved.
*
*/
#ifndef _b_h
#define _b_h
#include "deps.h"
class b
{
public:
private:
a *aclass;
};
#endif
if you uncomment the class declarations it will compile fine.
anyways, just something to take note of.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Item class
yeah, I realized that after a while of getting problems with different files calling others that... ah whatever, its just a big pain if you don't