0

In a YouTube video someone made two classes in Java like this:

public class Var {
    static JFrame jf1;
    static int screenWidth = 800;
    static int screenHeight = 600;

    public Var() {
    }
}
public class Gui {
    public Gui() {
        Var.jf1 = new JFrame();
        Var.jf1.setSize(Var.screenWidth, Var.screenHeight);
    }
}

As you can see he can access jf1 by just putting Var. in front of the variable.

Can you access member variables in C++ like this as well? Or do I have to create a GetValue function which returns the variable I want to have in a different class?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
andyyy
  • 23
  • 3

1 Answers1

3

In C++, you need to use Var::jf1. The . syntax is used when you have an object on the left side.

Also, you will need to define the member in your .cpp file:

JFrame Var::jf1;

Same for the other members.

MSalters
  • 173,980
  • 10
  • 155
  • 350