2

In C++ how may I write a function that is being called using the class name? for example if I have a class called test I want to call a function called calc like this:

test::calc();

and not via an object of the class.

  • Does this answer your question? [How do I call a static method of another class](https://stackoverflow.com/questions/4365982/how-do-i-call-a-static-method-of-another-class) – dfrib Jun 06 '20 at 11:52

2 Answers2

2
class test{
public:
   static void calc(){ /*do stuff */ }
};

See static members

systemcpro
  • 856
  • 1
  • 7
  • 15
1
 class Test {
 public:
     static void calc() { /* ... */ }
 };

 int main() 
 {  
    Test::calc();
 }

Essentially an ordinary function within the namespace of the class.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78