Jump to content
  • Advertisement
Sign in to follow this  
Android_s

Removing global variables, class acces problem

This topic is 4866 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

I'm currently trying to clean up my game code a bit, but i've got to a point where i can't see the best sollution. My problem is this: I have class A that's implemented into class B. Now class A need to access functions and variables in class B. I temprarily solved this with a global pointer back to class B, but this sollution is no longer possible, so i need another. Is there any clever way to do this without using singletons and such, or is there any way to re-write so this would be possible? I suppose one way would be to pass class B instance as a const ref to class A's constructor, but this seem...a bit wrong... Any help highly appreciated! =)

Share this post


Link to post
Share on other sites
Advertisement
So Class B is contained with Class A, but needs to call some od Class A's functions? I'm assuming you are using C++ also?

Why not pass Class B a reference to the Class A? It could then use that reference for making calls to its heart's content.

Share this post


Link to post
Share on other sites
Thanks for your fast reply =)

Yes that would be one way, and the only way i can think of...
I still wonder, however, if there's a "better" solution, or if this is considered "standard" procedure?
I'm not that skilled in all design patterns and were wondering if there were something that would help me?

Quote:

I'm assuming you are using C++ also?

At the moment i am, but i will have to port the code to J2ME soon...therefore i need to remove all globals =)

Share this post


Link to post
Share on other sites
A class that depends on the class that contains it might indicate a problem with the design. Other than that, passing a reference to B to A's constructor is the better solution (though you are likely to get warnings).

Share this post


Link to post
Share on other sites
Thank you for the answear.
I decided to use a singleton after all, however i have this problem in a few other places, and there i'll probably do what's suggested.

Quote:

A class that depends on the class that contains it might indicate a problem with the design.

You have no idea how right you are. I've made major changes to make this messy code into a more managable mess... =)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!