# (c++) best way to return an array in a class?

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to decide on how to return an array from inside a class. The class returns two variables: char keyState[256]; DIMOUSESTATE mouseState; For the mouseState in which DIMOUSESTATE is a structure, I think the best would be to do: DIMOUSESTATE getMouseState(){return mouseState;} I want the getKeyState function to be consistent with the getMouseState(). So I thought I could do two things, neither I can decide on: void getKeyState(char * pKeyState[256]){memcpy(pKeyState, &keyState, sizeof(keyState));} ^I don't really like this one, as it is not consistent with the getMouseState or: char * getKeyState(){return &keyState;} ^This one I thought might be not a super idea as the buffer's variables can be modified (not that i really care though). Any thoughts on which I should do, or if there is another way? thx edit: the only thing i like about java over c++ is that it is ok to return new pointers.

##### Share on other sites
0° Why is your key state a char, and why are character states stored as an array? Is std::map<key_t,state_t> not a lighter and more flexible alternative (and an easy one to return, at that)?

1° You want to return an unmodifiable array, preferably by reference. My reflex would be:

typedef const state_t keystates[256];
const keystate& getKeyState();

2° An alternative would be to let the function do the indexing, so it only returns the state for the requested key.

state_t getKeyState(key_t);

##### Share on other sites
How about do it in C++?
  class input_states  {  public:    typedef std::vector<char> key_state_t;    const key_state_t& key_state() const { return m_key_state; }  private:    key_state_t m_key_state;  };

Better yet, encapsulate your code for obtaining the state of a given key into your class. That way, an external entity doe not need to know anything about the internals of your object. That's the basis of OO.

--smw

##### Share on other sites
Theyre in char because I believe that is how directinput8 does it, well the last time I checked anyway.

I will return a const reference then.

Though I am a little confused on the two const, doesn't having the const in either position do the same thing?
Quote:
 Original post by Bregmaconst key_state_t& key_state() const { return m_key_state; }

Quote:
 Original post by BregmaBetter yet, encapsulate your code for obtaining the state of a given key into your class. That way, an external entity doe not need to know anything about the internals of your object.

Oh those functions were meant to be shown as part of the input class.

Quote:
 Original post by ToohrVyk2° An alternative would be to let the function do the indexing, so it only returns the state for the requested key.state_t getKeyState(key_t);

I've considered this, but I'm making my classes pretty bare bones and that removes some of the ease of being able to store a 'previous state' so I can check for key ups.

thanks!

##### Share on other sites
Original post by johnnyBravo
Though I am a little confused on the two const, doesn't having the const in either position do the same thing?
Quote:
 Original post by Bregmaconst key_state_t& key_state() const { return m_key_state; }

The first const is a part of the return value type. It says you can't modify the object returned by the function. The second const is a part of the function type. It says calling the function will not modify the object on which it's called.

--smw

##### Share on other sites
Quote:
 Original post by BregmaThe first const is a part of the return value type. It says you can't modify the object returned by the function. The second const is a part of the function type. It says calling the function will not modify the object on which it's called.--smw

To elaborate: generally in C++, you want to read types backwards, so:

const key_state_t& key_state() const; is a  ^       ^      ^      ----^    ^  |       |      |      |     constant  |       |      |  function that returns a  |       |   reference to a  |  key_state_t which isconstant

This is actually why I usually put const modifiers after the type, i.e. int const * const p (it reads more "naturally" - constant pointer to a constant int).

jfl.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631773
• Total Posts
3002270
×