1

So I am trying to make a C++ / OpenGL program (using Visual Studio 2010) that deals with Keyboard Input using a class called Operation. The point is to learn a few things since I'm new to both C++ and OpenGL.

I'm using an array 'bool keysDown[256]' to store which keys are pressed. Likewise I want to make an array of operations 'Operation *keyOps[256]', to operate those keys.

And here comes my problem, most of those keys will have no operation so I want an Operation class that will do absolutely nothing, but I only need one global instance/pointer that different files can use.

Now what I wanted to do, was to somehow create a single instance of this 'no operation' class and make it usable in any files that include this header without needing to declare it in each file. (Something like NULL, only in the shape of an Operation class)

So far my 'solution' was to use a namespace like this,

(operation.h)

#ifndef _OPERATION_H
#define _OPERATION_H

#include <iostream>

#include <GL\glut.h>

namespace operations{

    class Operation{
        protected:
            std::string _name;
        public:
            Operation(std::string name) : _name(name){}
            virtual void operate()=0;
            std::string getName(){return _name;}
    };

    (...)

    class OPnop: public Operation{
        public:
            OPnop(): Operation("No operation"){}
            void operate(){}
    };
    static OPnop OPNOP;
    Operation* nop(); //implemented in .cpp > {return &OPNOP;}

    (...)
};
#endif

Other files can get a pointer to the OPNOP instance by using the operations::nop() function. I've tested and it works, but I'm not sure this works as intended in the background.

I have been searching for extern and static usage on global variables, but I probably didn't understand it all and I didn't find an answer I could relate to my problem. If I'm not mistaken extern variables have to be declared in other files too while static creates a different variable for each file including it.

So my question is, is there a way to declare/instantiate this 'no operation' so that all the files including this header will have access to the same unique instance directly without having to use a function?

aslg
  • 1,966
  • 2
  • 15
  • 20
  • note that your include guard `_OPERATION_H` uses a [reserved identifier](http://stackoverflow.com/q/228783/283302) with an underscore followed by an uppercase letter. – Sam Miller Mar 28 '13 at 20:49

2 Answers2

3

Indeed the static keyword has a different meaning in the namespace context than the class context. I believe what you want to do is declare it as extern in your header, and in the implementation (.cpp) file initialize it once. Take a look at this question.

Community
  • 1
  • 1
Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
  • I made two externs, an instance and a pointer to that instance, then initialized them in the .cpp file. It worked thanks a lot! – aslg Mar 26 '13 at 11:57
0

I did something similar here.

I'm using a Gesture class with a single global instance as gGesture to handle all the user interactions.

    // .h file
    struct Gesture_{
        int fingers;
        int taps;
        eGestureAction action;
        bool continious;
    };
    typedef struct Gesture_ Gesture;

    extern Gesture gGesture;

To answer your question on static vs extern, the extern avoids linker problems by not adding same symbol to all translation units.

Notes:

  1. The code was originally intended to work in a C based project, but I think you'll get the idea.

  2. The Gesture object is intended for Touch based devices.

chunkyguy
  • 3,509
  • 1
  • 29
  • 34