-3

In java we can make a method of a class to be static by writing the keyword static before the type of the method : public static string some_method() { ... }

In C++ how to make a method static ?

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
pheromix
  • 18,213
  • 29
  • 88
  • 158
  • 1
    Just add a colon (`:`) after the `public`. – barak manos Dec 10 '14 at 15:55
  • 1
    I hope you're using [a good book](http://stackoverflow.com/q/388242/10077) to learn C++. – Fred Larson Dec 10 '14 at 15:55
  • 1
    @pheromix It may not be a bad idea to follow a good C++ book all the way to the end; this question would have been answered in there somewhere. – cdhowie Dec 10 '14 at 15:57
  • 2
    Even though I provided an answer below, this question could have been answered with a trivial web search. +1 for reading a good book or going through some other-media tutorial. -- edit: I added [java], as this is about Java-C++-Transition/Transcription. – Sebastian Mach Dec 10 '14 at 15:58
  • 1
    Would it be the case that all you need is just a global function which Java dose not have? –  Dec 10 '14 at 16:06

1 Answers1

6

You use the keyword static just like in Java.

With your example:

public static string some_method() { ... }

In C++:

public:
    static string some_method() { ... }

In this case, the colon (:) is the only difference; though in C++, public: designates all following functions as public, not only the one it is attached to.

However, please take notice that this answer just gives a fish, instead of teaching to fish. Depending on what you want to achieve, there are plenty alternatives in C++ to achieve something in a sane, idiomatic way.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130