Jump to content
  • Advertisement
Sign in to follow this  
MrDarkKnight

passing variable to multiple classes c++

This topic is 2536 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++.

Share this post


Link to post
Share on other sites
Advertisement
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
}

Share this post


Link to post
Share on other sites

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. :D

Share this post


Link to post
Share on other sites
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 '&').

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!