I want to put variables inside a class, and make it so every function inside that class can use and edite it. I can't make it to work... I want to call the variable inside other functions too (like int main), to check what's its state.
The first problem is you are not allowed to define values straight away when declaring them in a class. (unless they are const static variables, in which case you can't change them). If you want X to have a starting value then you will want to use a constructor to set it when the object is created. http://www.cplusplus.com/doc/tutorial/classes/
The other problem is that you are using static functions which can't change values in a class unless they are also static and are usually not called with class objects.
Assuming you don't actually need anything static, here is an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Example
{
public:
Example() {X = 27;}
int X;
void Cout()
{
cout << Example::X;
}
void ChangeX(int value)
{
Example::X = value;
}
}E;
(On a side note class variables are usually made private and you use public functions to access their value).
I think you are messing up with the distinction between instance and static variables. Try this example: can you see the difference in the behaviour of x (instance) and Y (static) ?
Hm, yes, I understand some of that. But, now that i see something new, what does Example(int v) : x(v) { } do? And what are you doing with Example a(1); Example b(10);? Does it have something to do with structures?
That's the way you a) initialise class members and b) invoke a constructor with params. For a) I think you can stick with the assignments (does it perform two assignments instead of one or am I wrong??).
LowestOne, i just noticed you're using C. I use C++ because sometimes is easier
It's not C. Because it has a class in it - It is definitely C++
Ignoring that, what i put after the function calling does't matter? So can i put "SelfCounter Hello;"?
SelfCounter first; // displays 1
That bit is creating an object called first which is an instance of SelfCounter. Not to be confused with a function call. It is calling a function - the constructor function that is part of the SelfCounter class, which is called every time an object of that type is created. The static variable remembers it's value, so when you create another instance of the class, you can do something with the retained value.