Item class

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Item class

Post by Bullet Pulse »

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.
Last edited by Bullet Pulse on Mon May 03, 2010 8:13 pm, edited 2 times in total.
Image
Live-Dimension
Chaos Rift Junior
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?

Post by Live-Dimension »

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.
Image
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Re: How to prevent infinite inclusion?

Post by Bullet Pulse »

Live-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.
Sorry, I changed the topic to something that will actually prevent the infinite inclusion. So please take a look at it :)
And yes have tried it.
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Item class

Post by avansc »

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.

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"
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Item class

Post by Ginto8 »

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.
The way I see it, it would probably be best to have an item function taking a parameter of a player class.
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.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Item class

Post by eatcomics »

avansc 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
that's what I do, works wonders for the life of a programmer :D

well actually I don't put classes in there
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Item class

Post by avansc »

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

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
a.h

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
b.h

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 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Item class

Post by eatcomics »

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
Image
Post Reply