C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2009, 02:37 PM   #1
Hail to the king, baby.
 
Akkernight's Avatar
 
Join Date: Oct 2008
Location: Faroe Islands
Posts: 713
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
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?

Last edited by Akkernight; 02-20-2009 at 02:52 PM.
Akkernight is offline   Reply With Quote
Old 02-20-2009, 03:00 PM   #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   Reply With Quote
Old 02-20-2009, 03:05 PM   #3
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-20-2009, 03:21 PM   #4
Hail to the king, baby.
 
Akkernight's Avatar
 
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();
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
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?
Akkernight is offline   Reply With Quote
Old 02-20-2009, 03:22 PM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-20-2009, 03:30 PM   #6
Hail to the king, baby.
 
Akkernight's Avatar
 
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
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?
Akkernight is offline   Reply With Quote
Old 02-20-2009, 03:31 PM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
http://apps.sourceforge.net/mediawik...llocating_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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-20-2009, 04:23 PM   #8
Hail to the king, baby.
 
Akkernight's Avatar
 
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;
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?
Akkernight is offline   Reply With Quote
Old 02-20-2009, 04:26 PM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-20-2009, 04:55 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 02-20-2009, 05:12 PM   #11
Hail to the king, baby.
 
Akkernight's Avatar
 
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
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?
Akkernight is offline   Reply With Quote
Old 02-20-2009, 05:14 PM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-20-2009, 06:03 PM   #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. 
};
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.
matsp is offline   Reply With Quote
Old 02-20-2009, 06:10 PM   #14
Hail to the king, baby.
 
Akkernight's Avatar
 
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() ?
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?
Akkernight is offline   Reply With Quote
Old 02-20-2009, 06:14 PM   #15
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Reply

Tags
classes, pointer

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:54 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22