0

I got this:

// mouse.h
class Mouse {
  private:
    struct Pos {
      static GLfloat x;
      static GLfloat y;
    };    
    static Pos last;
}

and this:

// mouse.cpp
// 1)
Mouse::Pos Mouse::last = {};
// 2)
Mouse::Pos Mouse::last = { 0.0, 0.0 };
// 3)
Mouse::last.x = 0.0f;
Mouse::last.y = 0.0f;

1), 2) and 3) are the attempts I've made at initializing that thing. I understand that the header should declare that last is static, and that the source should initialize it, but something has been wrong with all my attempts. Could someone please tell me the correct way to do such a thing? Am I missing some very important point? Is this nonsense? It is important that the fields are static. Thanks.

3
  • 1
    something has been wrong is not a useful problem description. What, exactly, is wrong? Commented Nov 25, 2016 at 12:18
  • 1
    Not sure what you're trying to do, but at least the GLfloats in the embeded struct should most likely not be static.
    – DrPepperJo
    Commented Nov 25, 2016 at 12:21
  • The Pos struct is used as a "namespace".
    – KIIV
    Commented Nov 25, 2016 at 12:26

2 Answers 2

7

You don't need to declare Pos content as static.

// mouse.h

class Mouse {
  private:
    struct Pos {
      GLfloat x;
      GLfloat y;
    };    
    static Pos last;
}

Mouse::Pos Mouse::last = { 0.0, 0.0 };

This should work too

2

It is important that the fields are static.

Then last will not have any state. It will simply refer to the static x and y values inside Mouse::Pos.

#include "mouse.h"

GLfloat Mouse::Pos::x = 10;
GLfloat Mouse::Pos::y = 10;

Mouse::Pos Mouse::last{};

wandbox example


The following asserts pass:

assert(Mouse::last.x == 10);
assert(Mouse::last.y == 10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.