Skip to main content
gamedev.net
🔒 Locked

constructors

Started by buggy123 Dec 15, 2009 at 10:25 PM 2 replies 1k views
Original Post
buggy123
buggy123
I'm using SFML and created a Input class for my game. I created a reference to sf::Input as one of the members of my class. But it kept complaining and told me that there is no appropriate default constructor and I solved the problem (it made me)by making my own constructor and initialize the reference to sf::Input in the constructor. But now I can't acess the member anymore.(grr) thus it's useless. How can I fix this?

class CInput
{
	public:

		CInput(sf::RenderWindow& app)
		{
			const sf::Input &User_input = app.GetInput();
		};
	
	private:
		
		

		struct mouse
		{
			int mouse_x;
			int mouse_y;
			bool lmb;
			bool rmb;
		};
};

CInput Input(Game.App);

btw, how can I acess members of the mouse struct?
jpetrie
jpetrie
Quote:

I'm using SFML and created a Input class for my game. I created a reference to sf::Input as one of the members of my class. But it kept complaining and told me that there is no appropriate default constructor and I solved the problem (it made me)by making my own constructor and initialize the reference to sf::Input in the constructor.

You're being told this because references, in C++, cannot be uninitialized or null (legally). If you have a class that contains a member that is a reference type, it is not possible for the compiler to generate a default constructor for that class, because it would be unable to initialize the reference. You must initialize that reference yourself, in a constructor of your own authoring. It can be a default constructor.

Quote:

But now I can't acess the member anymore.(grr) thus it's useless. How can I fix this?

You cannot access the member because you removed the member; your code sample does not contain any class members.

This is what you want to do:
class CInput {  const sf::Input& input;  CInput( sf::RenderWindow& app ) : input( app.GetInput() ) { }};


Quote:

btw, how can I acess members of the mouse struct?

The same way you access the members of any class or struct. You first need an instance of that type, and then you access them with instance.foo, instance.bar, et cetera.
buggy123
buggy123
Ohhh I see. I'm not really good with constructors (or deconstructors) Can some one give me examples of when they are needed?

Also, what does the colon in the middle do. I'm unfamiliar with the syntax

CInput( sf::RenderWindow& app ) : input( app.GetInput() ) { }

};

thanks
fastcall22
fastcall22
It's an initializer list, basically its a series of constructors for your member variables.

Topic Locked

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

Sign in to reply to this topic.