![]() |
| | #1 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 713
| problem with classes and pointers Well, I have this class called player. Code: class player : public character{
public:
player();
~player();
float speed;
private:
//hgeQuad
};
Code: class character {
public:
character();
~character();
float x, y;
static const int gravityPull = 1;
void gravity();
bool gravEffected();
};
I still havn't figured out the differences between the two calls... The thing I'm having problem with is the x and y from character, by the way. Thanks in advance Last edited by Akkernight; 02-20-2009 at 02:52 PM. |
| Akkernight is offline | |
| | #2 |
| Registered User Join Date: Feb 2009
Posts: 138
| when you use a pointer, are you allocating memory to it or pointing it to an object that already exists? you have to do one of those before you can use the pointer. |
| Meldreth is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| I assume you are doing this? Code: player* p1; p1->whatever(); A pointer is a variable that contains a memory address. It does not create storage; it must be pointed to a valid storage before use.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #4 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 713
| Well, I was doing this Code: if(hge->Input_GetKeyState(HGEK_LEFT)) P1->x -= P1->speed * dt; if(hge->Input_GetKeyState(HGEK_RIGHT)) P1->x += P1->speed * dt; if(P1->gravEffected()) P1->gravity(); Code: if(hge->Input_GetKeyState(HGEK_LEFT)) P1.x -= P1.speed * dt; if(hge->Input_GetKeyState(HGEK_RIGHT)) P1.x += P1.speed * dt; if(P1.gravEffected()) P1.gravity(); |
| Akkernight is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Does P1 point to a valid instance?
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #6 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 713
| huh? o.O P1 is declared like... player * P1, or was... Now it's: player P1 You see the player class on the thread start |
| Akkernight is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
|
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #8 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 713
| That really doesn't tell me anything at all xP I've figured out what my real question is now xD When should I use pointers to classes? Like, if I have a class that I'll use often, like player (there'll be atleast 2 players) should it then be pointed to or not? like what is more right: player* P1; player* P2; Or: player P1; player P2; |
| Akkernight is offline | |
| | #9 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Use pointers to point to the same object. Like when you need to keep references to objects. When you do not need pointers, do not use them.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #10 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #11 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 713
| Awsome! Thanks ![]() Pointers are really simple when you think of it, but still they're a pain in the ass sometimes xD |
| Akkernight is offline | |
| | #12 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| But as said, pointers do not actually create objects. They will point to them, and it's your job to thus create objects and make sure those pointers actually point to those objects later. There's the difference.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #13 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Also, in a case where inheritance is involved, pointers are important to manage objects of different types. Let's say we have: Code: class character {
public:
character();
virtual~character();
float x, y;
static const int gravityPull = 1;
void gravity();
bool gravEffected();
};
class player: public character
{
... stuff here.
};
class monster: public character
{
.... other stuff here.
};
// Helper to the player - a "robot" that helps the player.
class helper: public character
{
... stuff to keep track of a player and "help" the player.
};
Also note that teh destructor of any type that inherits should be virtual. Otherwise, any case where you GENERICALLY destroy an object, it will call the wrong destructor. [And by the way, I'm by no means saying that your monsters should be based on character - I was just lacking in imagination as to what type of classes could derive from character, and monster was the first one to come to mind]. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #14 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 713
| Well, this game won't have monsters exactly, it'll have players, then I'll make computer AI players too, if I can figure how ![]() But I'll be putting them under character too, shouldn't I be doing this? And I should add virtual to the ~character() ? |
| Akkernight is offline | |
| | #15 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Whether you should or should not inherit from the same base for the computer controlled and the human-controlled players, I can't answer, as I don't know if a "AI player" is-a character or not. If it is, then, yes, it should inherit. It isn't, then you shouldn't... -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
![]() |
| Tags |
| classes, pointer |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Q: Pointers to Classes '->' notation | Howie17 | C++ Programming | 2 | 12-12-2008 10:09 AM |
| destructors and parameters + pointers to classes questions. | Zigs | C++ Programming | 12 | 12-31-2007 06:59 AM |
| two-dimensional dynamic array of pointers to classes | Timo002 | C++ Programming | 4 | 04-21-2005 06:18 AM |
| Pointers to Classes || pointers to structures | C++Child | C++ Programming | 24 | 07-30-2004 06:14 PM |
| Help With pointers in classes. | indigo0086 | C++ Programming | 12 | 10-10-2002 02:03 PM |