Skip to main content
GameDev.net gamedev.net
🔒 Locked

passing variable to multiple classes c++

Started by MrDarkKnight Jan 3, 2012 at 3:16 PM 2 replies 1.3k views
Original Post
MrDarkKnight
MrDarkKnight
Hello

I have a three classes called Player, Enemy and Bullet and finally my main function.
now in every class there is a function to get their X and Y position. I call all the classes in the Main function and they work perfectly. Now my problem is if I just want to call the Player.GetXPosition() in Enemy class or Bullet class I get 0. But if I call Player.GetXPosition() in the Main function I get the value.

This problem has been hunting me in all of my projects and its driving me crazy. Now I think i can solve this problem by passing the Memory address of Player.GetXPosition() to Enemy class or Bullet class and I'll get the value, but i don't know how to do this.

please guys help me.

BTW i'm using c++.
brx
brx
I have the feeling that there is some kind of misunderstanding about the Class/Object concept. You call methods (functions) on objects not on classes. Classes are only a definition of what an object can do (at least in C++, in other languages classes themselves are objects as well, but that doesn't matter right now). So if you call the Player::getXPosition() method you need an instance (object) of the Player class.

As you did not post any code, I assume you are doing something like this (very very simplified, but I hope you'll get the picture):


// main
int main(int argc, char** argv) {
Player p;
p.SetXPosition(5);
int playerX = p.GetXPosition(); // returns 5
// more code. ...
}

// somewhere in your Bullet class
void Bullet::someMethod() {
Player p; // creates a new instance of the class Player totally unrelated to the one created in your main method
int playerX = p.GetXPosition(); // returns the default value for x (apparently 0)
// more code ....
}

So what you need to pass to the method in Bullet that is supposed to use the Player object is the actual instance of the Player class. E.g.:

void Bullet::someMethod(Player& p) {
int playerX = p.GetXPosition(); // returns whatever the player's x position was set to outside
// more code...
}

Going back to your main function it may look like this:

// main
int main(int argc, char** argv) {
Player p;
p.SetXPosition(5);
Bullet b;
b.someMethod(p); // will use the Player object with X position set to 5
}
MrDarkKnight
MrDarkKnight

I have the feeling that there is some kind of misunderstanding about the Class/Object concept. You call methods (functions) on objects not on classes. Classes are only a definition of what an object can do (at least in C++, in other languages classes themselves are objects as well, but that doesn't matter right now). So if you call the Player::getXPosition() method you need an instance (object) of the Player class.

As you did not post any code, I assume you are doing something like this (very very simplified, but I hope you'll get the picture):


// main
int main(int argc, char** argv) {
Player p;
p.SetXPosition(5);
int playerX = p.GetXPosition(); // returns 5
// more code. ...
}

// somewhere in your Bullet class
void Bullet::someMethod() {
Player p; // creates a new instance of the class Player totally unrelated to the one created in your main method
int playerX = p.GetXPosition(); // returns the default value for x (apparently 0)
// more code ....
}

So what you need to pass to the method in Bullet that is supposed to use the Player object is the actual instance of the Player class. E.g.:

void Bullet::someMethod(Player& p) {
int playerX = p.GetXPosition(); // returns whatever the player's x position was set to outside
// more code...
}

Going back to your main function it may look like this:

// main
int main(int argc, char** argv) {
Player p;
p.SetXPosition(5);
Bullet b;
b.someMethod(p); // will use the Player object with X position set to 5
}



thank you very much that was very helpful.
you really made my day thank you sooooo much.
you have no idea how easy you just made my life.
brx
brx
No problem, glad I could help.

One thing I forgot to mention are that there are different ways of passing arguments in C++. I recommend you look up "pass by value/reference/pointer". In the example I wrote the Player object is passed by reference (noted by the '&').

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.