initializing and passing a reference (static ?)
Feb 9, 2009 at 3:36pm UTC
The code below compiles fine.
The only problem is that I do not want to pass the reference each time.
I would like to set the reference to the BaseApp only once, and then access it through the created GH_Home_KM or GH_Calibtration_KM.
The problems are :
1) If I create a GH_KM::GH_KM() constructor, I am forced to initialize the myBase reference (which is normal, but I don't know to what I should initialize it)
2) I cannot make the myBase reference static.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//=> file : BaseApp.h
#ifndef BASEAPP_H
#define BASEAPP_H
#include "OIS.h"
class GH_Home_KM;
class BaseApp
{
public :
BaseApp();
~BaseApp();
friend class GH_KM;
private :
OIS::Keyboard *mKeyboard;
std::auto_ptr<GH_Home_KM> home_keyMap;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
//=> file : GH_KeyMapping.h
#ifndef GH_KEYMAPPING_H
#define GH_KEYMAPPING_H
#include "OIS.h"
#include <iostream>
class BaseApp;
class GH_KM : public OIS::KeyListener
{
public :
//GH_KM();
GH_KM(BaseApp& ba);
virtual bool keyPressed(const OIS::KeyEvent &e);
protected :
static std::map<std::string,GH_KM *> keyMapsCollection;
BaseApp& myBase;
};
class GH_Home_KM : public GH_KM
{
public :
GH_Home_KM(BaseApp& ba);
virtual bool keyReleased(const OIS::KeyEvent &e);
};
class GH_Calibration_KM : public GH_KM
{
public :
GH_Calibration_KM(BaseApp& ba);
virtual bool keyReleased(const OIS::KeyEvent &e);
};
#endif //GH_KEYMAPPING_H
1 2 3 4 5 6 7
//=> file : BaseApp.cpp
#include "BaseApp.h"
#include "GH_KeyMapping.h"
BaseApp::BaseApp(): home_keyMap(new GH_Home_KM(*this )){}
BaseApp::~BaseApp(){}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//=> file : GH_KeyMapping.cpp
#include "GH_KeyMapping.h"
#include "BaseApp.h"
std::map<std::string,GH_KM *> GH_KM::keyMapsCollection;
//- KEYMAP CLASS
//GH_KM::GH_KM(){}
GH_KM::GH_KM(BaseApp& ba): myBase(ba){}
bool GH_KM::keyPressed(const OIS::KeyEvent &e){return true ;}
//- HOME KEYMAP
GH_Home_KM::GH_Home_KM(BaseApp& ba): GH_KM(ba){}
bool GH_Home_KM::keyReleased(const OIS::KeyEvent &e)
{
std::cout<<"pressed key in Home Modus" <<std::endl;
return true ;
}
//- CALIBRATOR KEYMAP
GH_Calibration_KM::GH_Calibration_KM(BaseApp& ba): GH_KM(ba){}
bool GH_Calibration_KM::keyReleased(const OIS::KeyEvent &e)
{
std::cout<<"pressed key in Calibration Modus" <<std::endl;
return true ;
}
Topic archived. No new replies allowed.