Accessing class object member pointer variables

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Accessing class object member pointer variables

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Accessing class object member pointer variables

Post 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".
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Re: Accessing class object member pointer variables

Post 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*'
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Accessing class object member pointer variables

Post by Ginto8 »

well then you did something wrong in creating the Bullet class. Mind posting some code?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Re: Accessing class object member pointer variables

Post 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
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Accessing class object member pointer variables

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Re: Accessing class object member pointer variables

Post by TheThirdL3g »

Ohh that makes sense. Thanks again for you help.
Post Reply