-3

I am writing a lot of java now, so I am getting confused with the java static methods and c++ static functions.

in java, you can call a static methods from a class, and I frequently use/see it, for exmaple:

public class A{
    public void static b(){
        System.out.println("hello");
    }
}

You can do, A.b(); Can you do that in C++? If so, is it not so popular as compared to doing so in java?

Vincent van der Weele
  • 12,682
  • 1
  • 31
  • 60
HoKy22
  • 3,677
  • 6
  • 31
  • 51

2 Answers2

5

You can do that, in C++ using the :: scope operator:

A::b();

And as pointed out, if you are having an instance a of your class A in the current scope you can also call a.b(). Calling a static method on an instance is usually confusing though, so you might want to avoid it.

Shoe
  • 72,892
  • 33
  • 161
  • 264
2

You can use A::B()

you can also use a.B() if a is an instance of A. However that is just confusing for someone who reads the code. So just stick to A::B() for static methods to clearly show what you meant with it.