Thread: problem with classes and pointers

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    problem with classes and pointers

    WARNING: Not very clean code xP

    Well, I have this class called player.
    Code:
    class player : public character{
    
    public:
    	player();
    	~player();
    
    	float speed;
    
    private:
    	//hgeQuad
    
    };
    And then I have this class called character.

    Code:
    class character {
    
    public:
    	character();
    	~character();
    
    	float x, y;
    	static const int gravityPull = 1;
    
    	void gravity();
    	bool gravEffected();
    
    };
    And in the main.cpp file I call player* P1, this crashes the game, and I've had laods of problems, when I call it as a pointer, but not when I call it like; player P1, why is this?
    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.
    Currently research OpenGL

  2. #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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I assume you are doing this?
    Code:
    player* p1;
    p1->whatever();
    If yes, then Meldreth has already told you the answer.
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    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();
    Now I'm 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();
    the first one didn't work
    Currently research OpenGL

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does P1 point to a valid instance?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    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
    Currently research OpenGL

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    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;
    Currently research OpenGL

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Akkernight View Post
    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;
    The first example has 0 players; the second example has 2 players. Which do you want?

  11. #11
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Awsome! Thanks
    Pointers are really simple when you think of it, but still they're a pain in the ass sometimes xD
    Currently research OpenGL

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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. 
    };
    Now, in some cases, you will certainly KNOW which type you are dealing with, but in other places, you may well have a situation where you do not know what sort of derived character you are dealing with. In these situations you MUST use a pointer or reference that refers to the actual character.

    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.

  14. #14
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    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() ?
    Currently research OpenGL

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Akkernight View Post
    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() ?
    Yes, if you inherit from a class, the destructor should be virtual. That's a rule.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q: Pointers to Classes '->' notation
    By Howie17 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2008, 10:09 AM
  2. Replies: 12
    Last Post: 12-31-2007, 06:59 AM
  3. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  5. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 02:03 PM

Tags for this Thread