1

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
  • 816
  • 1
  • 7
  • 14
1
 class Test {
 public:
     static void calc() { /* ... */ }
 };

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

Essentially an ordinary function within the namespace of the class.

Ayxan Haqverdili
  • 23,309
  • 5
  • 37
  • 74
Michael Chourdakis
  • 9,693
  • 2
  • 38
  • 66