• Advertisement
Sign in to follow this  

Little help needed

This topic is 1779 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 class "Input", that many other classes need( Would use ) and i need only one instance of class "Input" and i don't want to pass it around as reference all over the place.

Suggestions please.

 

Currently the class looks like this

 

class Input

{

public:

    void SetupInput(fonts &objFon);


    sf::RectangleShape recBackground;

    sf::Text txt;

};
Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Advertisement

Well, if you don't want to pass it around as a reference and you only need one instance of it then you can just make a static Input object. Then, all you have to do is use the static object instead of passing it as a reference everywhere.

I'm not sure how you would do it in C++. 

Have a main.cpp or something then declare a namespace, then inside that namespace have a static Input object then all you have to do is: namespaceName::input.something().

Not sure if that'll work though, I'm not very proficient in C++.

Share this post


Link to post
Share on other sites

Singleton (not the best choice? but still can be used for what you want) or Global variable in namespace.

Share this post


Link to post
Share on other sites

Singleton suggestion (single, global class) is most likely a correct answer to your question.

 

However, I wonder what your input class actually do. Maybe this particular case can be elegantly solved without a global variable.

Share this post


Link to post
Share on other sites
I would assign an Input to a global variable inside one .cpp file, and have the header of that cpp file declare the variable as extern. Then only the files that include that header can see it.

example:
main.cpp:
#include "main.hpp"
#include "input.hpp"
Input in;
int main(int argc, char ** argv)
{
//initialize in
}
main.hpp:
#ifndef MAIN_HPP
#define MAIN_HPP
#include "input.hpp"
extern Input in;
#endif
other.cpp:
#include "main.hpp"
void function(void)
{
//do things with the variable in
}

Share this post


Link to post
Share on other sites

You can create a base class the has static members which pertain to the input aspects.  Because static memebers are visible to all the members of the class, these static members only get intialized once, but are available to all classes that derive from the input base class. 

 

To destroy this class you could do several things.  Call a function called TerminateInput() that shuts down all the static members, or out the code into the destructor. 

 

In all honesty though, input is usally just  a player thing.  I don't see why you would need to do this.

Share this post


Link to post
Share on other sites

I should describe what the class will actually do.
Basically what it will be used for is to get input that will open files.

In class Map a user presses "OpenMap" Input class should get called to get input, when user is done typing the file name i need to get the string that holds the file name and open it in map.

Same will happen in class TileSheet or class Music.
 

I will probably go for singleton class.

Am looking for other ways to do this for learning. Thanks on all the posts

Share this post


Link to post
Share on other sites
One way to avoid having a global for this is to use the Model-View-Controller pattern.

In this scheme the Map would not handle user input but would simply have a LoadMap(name) function. A controller class would handle the UI. You can store any UI state (such as input strings) in the controller class, and the Map would not need access because the controller would call the LoadMap() function.

Share this post


Link to post
Share on other sites

One way to avoid having a global for this is to use the Model-View-Controller pattern.

In this scheme the Map would not handle user input but would simply have a LoadMap(name) function. A controller class would handle the UI. You can store any UI state (such as input strings) in the controller class, and the Map would not need access because the controller would call the LoadMap() function.

Interesting.

Share this post


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

  • Advertisement