Advertisement

One more quick Question on THIS->

Started by August 02, 2002 12:18 PM
5 comments, last by Radagar 22 years, 5 months ago
Take this for example...

class Cat
{
public:
   Cat();
   ~Cat();
   Init();
   int age;
   int weight;
};

void main()
{
   Cat Felix;
   Felix.Init();
   cout<age = 1
   weight = 2;                    this->weight = 2
}                              }
 
Does it make a difference if you use the 'this' pointer in a situation like this? When is the 'this' pointer most useful? Thanks! ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~ [edited by - Radagar on August 2, 2002 1:19:16 PM]
WyrmSlayer RPG - In Early Development
the "this" pointer is useful in a miscellaneous laundry-list of situations.

For the situation you described, there is absolutely no difference to the compiled code between the two. Since member functions let you refer to class members directly, you may as well do so. No reason to complicate the issue with pointers.

The "this" pointer is mainly useful when a member function needs to tell some non-member function about the class it''s in.


Don''t listen to me. I''ve had too much coffee.
Advertisement
Ok, my program is crashing and I''m not sure why.. This might be kind of long, but hopefully you can help..

I have a Player Class (derived from Creature)
I have a Weapon Class (derived from Item)

Player has a Pointer to a Weapon

Weapon has a member function called Equip(int) where
int is the number of the item. Equip then sets up the stats
on the item, such as Name, Weight, and Value.

Player has a member function called Init(). Init sets up the Player initial stats as well as Equip(1) (equips a dagger). The program crashes when it calls Equip(1). Here is some of the code.

From BM.cpp

  int main(){   Player player;   Player* theplayer = &player //Not used in this example   player.InitChar();   player.ViewChar();}  


From Class.h

  class Item{public:	char* name;	int id;	int weight;	int value;};class Weapon : public Item{public:   Weapon();   ~Weapon();   Equip(int);   int damage;   bool two_handed;};class Creature{public:	char* name;	int hp;	int maxhp;	int mp;	int maxmp;	int Str;	int Dex;	int Wis;	int Int;	int Con;	int Cha;	Weapon* weapon;	Armor* body_armor;	Armor* leg_armor;	Armor* arm_armor;	Armor* hand_armor;	Armor* foot_armor;	Armor* head_armor;	Armor* back_armor;	Armor* shield;};	class Player : public Creature{public:	Player();	~Player();	InitChar();	ViewChar();	char* race;	char* Class;	int gold;	int level;	int experience;	int expneeded;	Item* inventory[10];};  


From Class.cpp

  Player::InitChar(){	Str = 15;	Dex = 15;	Int = 15;	Con = 15;	Wis = 15;	Cha = 15;	gold = 500;	hp = 10;	maxhp = 10;	mp = 10;	maxmp = 10;	experience = 0;	expneeded = 100;	Class = "Warrior";	race = "Human"; //This Works	cout<<"race set"<<endl;	weapon->Equip(1);                 //Program Crashes Here	cout<<"weapon equipped"<<endl;	cout<<"weapon is "<<weapon->name;  


Also from Class.cpp

  Weapon::Equip(int id){switch(id)   {	case 1: //Rusty Dagger		cout<<"are we getting here?"; //gets Here		name = "Rusty Dagger"; //Crashes on This Line		cout<<"how about here?";		damage = 3;		two_handed = false;		value = 2;		weight = 1;		break;	case 2: //Dagger		name = "Dagger";		damage = 4;		two_handed = false;		value = 5;		weight = 1;		break;	default:		name = "Erroneous Lightsaber";		damage = 50;		two_handed = false;		value = 32764;		weight = 3;		break;   }}  


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
You''re calling member functions on weapon without first initializing the pointer. Use something like this:

weapon = new Weapon();

Of course, don''t forget to delete it in the destructor.


Don''t listen to me. I''ve had too much coffee.
quote: Original post by Sneftel
You''re calling member functions on weapon without first initializing the pointer. Use something like this:

weapon = new Weapon();

Of course, don''t forget to delete it in the destructor.


Don''t listen to me. I''ve had too much coffee.



WOOHOO!! Thanks Sneftel, that fixed it. And even better, something dinged in there. I now understand where new and delete can be used somewhat =)





~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
Another thing you need to understand is where to use pointers and where not to.

Why not change that Weapon * to a Weapon? That way, it''ll automatically be created and destroyed.


Don''t listen to me. I''ve had too much coffee.
Advertisement
quote: Original post by Sneftel
Another thing you need to understand is where to use pointers and where not to.

Why not change that Weapon * to a Weapon? That way, it''ll automatically be created and destroyed.


Don''t listen to me. I''ve had too much coffee.


*thinks*...

there was a reason to use pointers there I thought... But I sure can''t see a reason why I would need the player to have a pointer to a weapon class instead of a direct weapon class...

*thinks*

I think in the beginning I was going to keep a list of all the weapons/armor/items in the game and have the players equipment and inventory just be pointers to items on that list. but since I''m not doing it that way anymore, it sure would simplify things to do it without pointers..

*goes over to start tearing down his code*



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development

This topic is closed to new replies.

Advertisement