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.

1

2 Answers 2

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

See static members

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

Comments

1
 class Test {
 public:
     static void calc() { /* ... */ }
 };

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

Essentially an ordinary function within the namespace of the class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.