-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?

6
  • 3
    See Help with C++ static method Commented Apr 5, 2013 at 13:38
  • 3
    Stack overflow is not google Commented Apr 5, 2013 at 13:38
  • In C++, you would write A::b(); which does the same thing. Commented Apr 5, 2013 at 13:38
  • 2
    @Jason before posting a question some effort should be made. Commented Apr 5, 2013 at 13:39
  • @Joe agreed. Downvote Commented Apr 5, 2013 at 14:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you have an instance a of type A, you can also use a.b().
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.