Static Variables in classes

Hey there,
I am very new to object oriented programming, but I have written a simple class which has a method which returns the state of the button. However I would like to make it a toggle button, so I need to know the state the button was the last time the method was called. I have done some research on it, and it seems that static variables are the way to go.

But when I define the static variable like public or private variables like so:

static:
 boolean _statel

I get the following error: expected primary-expression before '_statel'

When I simply put it under the private vars like so:

public:
static _statel

It simply does not work, the variable does not seem to be stored.

Am I simply asking for something I am not intended to, or am I doing something wrong?

thanks for your help!

Javamaster10000

Create a private variable to store the state of the button. Use that variable from the functions of the class (because it is private). Forget about static. Show us a sketch that we can test.

Koepel:
Forget about static.

Only in regard to this problem.

@javamaster10000: Please lookup the syntax and semantics of defining a static class member.

I disagree.

A static function variable behaves very much like a static class variable,
if the function is static, it's just the same.

The local variable will only be visible in the function, that's the main difference.
All instances of the class would share the same variable.

As Koepel already wrote, use a normal class variable for the previous state.

Delta_G:
I didn't mean a static function, I just meant a local variable in a regular class function where the local variable was declared static.

But, you learn something every day. When I tested this sure enough the two classes shared the same variable even though it was local to a non-static function. I stand corrected.

You have to question why they couldn't have invented another keyword for static class members or static variables in a function...

maybe because there was just no better word to describe either case!

The meaning of the static keyword is very context sensitive, that's a little confusing.

It is a 'one function' to 'one variable' mapping for static local variables.

Just like the 'one class definition' to 'one variable' mapping for a static member.

javamaster10000:
But when I define the static variable like public or private variables like so:

static:

boolean _statel



I get the following error: expected primary-expression before '_statel'

Syntax is

static boolean foo;

'static' is a storage class specifier, 'boolean' is a type specifier.

Having said this 'static' means "there's only one of 'em". What happens if you want two toggle buttons? Using a class is the right approach. You want an ordinary member variable to hold the current/previous state.

I like using constructors to do pin assignments, and I like all of these classes to have their own setup() and loop() methods, which I call even if I don't do anything with them.

See my blog post ArduinoTheOOWay for more on how I personally go about doing this kind of stuff..

Okay...
So as it seems all static variables are shared across all objects of that type.

If this is the case, how am I supposed to store this value?

Javamaster10000

This was answered in #1, why do you ask again?

Koepel:
Create a private variable to store the state of the button. Use that variable from the functions of the class (because it is private). Forget about static.

javamaster10000:
But when I define the static variable like public or private variables like so:

static:

boolean _statel



I get the following error: expected primary-expression before '_statel'

Lose the colon. Add a semicolon at the end.

You can't just invent your own syntax for C++.

C++ supports:

static boolean foo;

Not:

static: boolean foo

javamaster10000:
So as it seems all static variables are shared across all objects of that type.

If this is the case, how am I supposed to store this value?

What do you mean by that?

I mean, if for example I create an object of that type, named button1 and an object named button2, all static vars are share across them, so the code would not work, is this correct?
someone pointed out the semicolon was missing at the end of the definitions, I actually have them in the code.

so the code would not work, is this correct?

No, the code WOULD work exactly the way that the language developers expected. That may not be the same way YOU expect, but when that happens, it only means that your expectations are wrong.

someone pointed out the semicolon was missing at the end of the definitions, I actually have them in the code.

Then THAT is the code you should have posted.

By now, you certainly should have gotten the message that a static variable is not what you (seem to) want.

Well, I understood, static vars are not what I want, but what should I do in this case?

but what should I do in this case?

Use private member field(s) (variables).

Create a normal class variable to store the state of the button.

Use that variable in the functions of the class.

class foo {

  bool oldState;

  bool sameAsOld(bool nowState)
  {
    return oldState == nowState;
  }
};

Whandall:
Create a normal class variable to store the state of the button.

Use that variable in the functions of the class.

class foo {

bool oldState;

bool sameAsOld(bool nowState)
  {
    return oldState == nowState;
  }
};

Where should I put this code?
And how do I mae sure each object gets its own variables?

javamaster10000:
Where should I put this code?

Nowhere. This is only an example for an imaginary class to show the concept in a more graphical way.

javamaster10000:
And how do I mae sure each object gets its own variables?

By not defining the member variable static?

I suggest you read some more about C++ classes.

javamaster10000:
I mean, if for example I create an object of that type, named button1 and an object named button2, all static vars are share across them, so the code would not work, is this correct?

Depends on what you mean by "work". If you want the variables shared, it will work. If you don't want them shared it won't work.

javamaster10000:
However I would like to make it a toggle button, so I need to know the state the button was the last time the method was called. I have done some research on it, and it seems that static variables are the way to go.

Why? If each class member has to remember the state of the button, then static variables are not appropriate.