Page 1 of 1

Accessing class object member pointer variables

Posted: Thu Dec 30, 2010 8:55 pm
by TheThirdL3g
I have class "Bullet" with x and y declared as public variables.

I need to create objects of the class within the program so I tried using a pointer:

Bullet *aBullet = new Bullet(ship.x, ship.y + 20, 10);

When trying to call the y variable (Bullet::bBullet.y <<< not sure if this is right) I get an error saying aBullet isn't a member of Bullet.

Any help would be appreciated.

Re: Accessing class object member pointer variables

Posted: Thu Dec 30, 2010 9:11 pm
by Ginto8
just do aBullet.y. Bullet::aBullet.y is saying to the compiler "get the static member of Bullet named aBullet, then get its member y", when what you want is just "get aBullet's member that is named y".

Re: Accessing class object member pointer variables

Posted: Thu Dec 30, 2010 9:15 pm
by TheThirdL3g
Thanks for the reply

Thats what I originally had, but it gave the error "request for member 'y' in 'aBullet', which is of non-class type 'Bullet*'

Re: Accessing class object member pointer variables

Posted: Thu Dec 30, 2010 10:42 pm
by Ginto8
well then you did something wrong in creating the Bullet class. Mind posting some code?

Re: Accessing class object member pointer variables

Posted: Fri Dec 31, 2010 12:05 am
by TheThirdL3g
If you need to do something extra for using the pointer then that's my problem.

Code: Select all

class Bullet {
public:
	float x;
	float y;
	float radius;
	void bDisplay();
	Bullet(float, float, float);
};


Bullet::Bullet(float xx, float yy, float rr) {
	x = xx;
	y = yy;
	radius = rr;
}

void Bullet::bDisplay() {
	glBegin(GL_LINE_LOOP);
	glVertex3f(x-(radius*0.5), y-(radius*0.5), 0.0f);
	glVertex3f(x+(radius*0.5), y-(radius*0.5), 0.0f);
	glVertex3f(x+(radius*0.5), y+(radius*0.5), 0.0f);
	glVertex3f(x-(radius*0.5), y+(radius*0.5), 0.0f);
	
	glEnd();
}

Thanks for the help

Re: Accessing class object member pointer variables

Posted: Fri Dec 31, 2010 12:12 am
by Ginto8
oh wait, you need to use -> instead of . because aBullet is a pointer. a->b is shorthand for (*a).b, so it dereferences the pointer for you.

Re: Accessing class object member pointer variables

Posted: Fri Dec 31, 2010 1:06 pm
by TheThirdL3g
Ohh that makes sense. Thanks again for you help.