user13659234
user13659234

Reputation:

C++, call function using class name

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.

Upvotes: 2

Views: 1587

Answers (2)

Michael Chourdakis
Michael Chourdakis

Reputation: 11178

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

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

Essentially an ordinary function within the namespace of the class.

Upvotes: 1

Jellybaby
Jellybaby

Reputation: 996

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

See static members

Upvotes: 2

Related Questions