Advertisement

class/pointer help

Started by May 25, 2000 01:04 PM
3 comments, last by OoMMMoO 24 years, 7 months ago
Can you use object pointers in a class for instance I have a class name item class item { public: char name[2]; char description[20]; }; class player { //player stuff here item *pitem;//for the item the player is holding }; if it is legal how do i access it do i go player character; item potion; *character.item=potion;??????? also can I make functions for each object?? like class enemy { public: void attack(); }; enemy ogre; can I then make a special attack for ogre like void ogre::attack() { //attack } ?????
Pointers in classes to other classes - yep.
How to access(from your example):
character.item=&potion // meerly assignes potions address to ''character.item''
- not the best way, especially if potion goes out of scope.
- one of the wizzes probably can show you a better way.

Making functions for eachvariable - no, but you can have a ogre class that inherits from enemy(class ogre::public enemy), and add, modify, or ignore it''s member functions (ie attack can be overridden by simply defining it without having to declare it again).
Advertisement
To access via a pointer you use -> rather than .
So, in your example you would say:

character->pitem= // whatever

You must remember to allocate memory for the item during initialization of a player-object, e.g.
this->pitem = new item;


-Neophyte
~Death awaits you all, with nasty, big, pointy teeth~
To assign to the pitem data member of an player object, do the following:

player character;
item potion;
character.pitem = &potion

However, when potion is a local object, the memory that character.pitem points to will be invalid. This is because when potion is unscoped, the object will be destroyed.

To get around this, use new and delete to allocate and deallocate memory for objects on the free store:

character.pitem = new item;
..
delete character.pitem;

To access the data members of potion, do as follows:

character.pitem->name
character.pitem->description

A better way to do this would be by using encapsulation: providing class methods to manipulate private data members. This is one of the things that C++ is about, so I suggest getting a good C++ book to learn about it.

As a small example, consider this slightly different item class:

class item{public:    // item ctor    item() : name_(0) { ; }    // item dtor    virtual ~item() { delete[] name_; }    // get item name    const char* get_name() const { return name_; }    // set item name    void set_name(const char* name)    { delete name_; name_ = new char[strlen(name)+1];      strcpy(name_, name); }private:    char* name_;} 

Here, you use the get_name and set_name member functions to retrieve/store the name of an item. If you were to change the implementation of the name (for example by using the std::string class), any code that uses the get_name and set_name functions doesn''t have to change.

As for special functions for objects, see Whirlwind''s reply. This is discussed in any good C++ book as well.

Erik
First of all, DAMN!
For someone who buys and has as many programming books as you do, you sure aren''t a good reader

I really don''t know what i can say, cuz your code from what i''ve seen is the most f**ked up thing.....
lemme tell you again...HELP ME WITH MINE;P

"There is someone gay in my Programming class, and its not my, Ryan, but his name starts with a ''B''."



-Run_The_Shadows
-Run_The_Shadows@excite.com

This topic is closed to new replies.

Advertisement