Access a method variable from another method

Is it possible to do that?

For example, is it possible to write something like:
1
2
3
4
5
6
7
8
9
10
11
12
class A
{
  public:
    void method1(void)
    {
      int a = 1;
    }
    void method2(void)
    {
      cout << "a from method1:" << a << endl;
    }
};


...which works, using some keywords/syntax I don't know?
You don't need special keywords for that. It's called member variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
class A
{
  int a; // Note: place the variable here
  public:
    void method1(void)
    {
      a = 1; // Note: no int before 'a'
    }
    void method2(void)
    {
      cout << "a from method1:" << a << endl;
    }
};
I know...but the problem is that I need to define a inside method1 and not outside...
You cannot be inside and outside at the same time. The only way is to call method1 which returns the value of 'a'
Ok, thank you!
Topic archived. No new replies allowed.