passing pointers of objects to other objects?

From my last thread I had a problem with my game of getting the shooting to work. I figure an easy fix would be to simply pass a pointer of the bulletholder object to the player object (tank) instead of routinely changing and reading a global int value.
so:
in tank.h:
class bulletholder:
class tank
{
public:
bulletholder *holder;
shoot();
};

in tank.cpp:
void tank::shoot()
{
holder.shoot(x,y,dir);
}

the reason I felt I needed a separate bulletholder object in the main is because I need that data to know where each bullet bitmap should be copied to the screen. I guess it wouldn't be too hard to simply have a bulletholder object in the tank object, and make a middleman method to pass to data along.

Now that I think about it I'm def doing the latter.

which method would be better? How would you do this?
Im not quite sure what you mean but inheritance maybe? Just a suggestion
There are basically two types of inheritance, "is a" and "has a".

Is a: class Rectangle : public Shape
Here a Rectangle is a Shape. Perhaps Rectangle holds the length of the sides, and Shape holds the Area. To continue in this direction: class Triangle : public Shape.

Has a:
1
2
3
4
5
6
class Student
{
private:
   Schedule schedule;  // The Schedule class is a private member

//... 

Here a Student has a Schedule. A student isn't a schedule.

So I think you have it right. A Tank has a Holder. Although, perhaps the Holder should have it's own x,y,dir variables, so that shoot does not need arguments.
OK I guess I wasn't being clear. Its Object containing object vs Object containing pointer to external object to pass data between the two and execute certain functions in a timely manner. Bulletholder and tank are not related. Data contained by both bulletholder and tank need to at some point be passed to main.cpp which is why I was wondering. I was more curious to know if its ok or at least valid to have a reference to an external object. Which isn't technically inheritance. bulletholder needs variables passed to it because it maintains a linked list which contains the data for all bullets fired from the tank.
What is a Bulletholder?
bulletholder is the class I created to manage the data for bullets fired by either player 1 or player 2. It uses a linked list for this.
Topic archived. No new replies allowed.