Bennett Kenyon
Bennett Kenyon

Reputation: 21

Non-Object Function/ Class Function C++

I've been playing with Java and C++ for a while now. In Java it's possible to have functions that don't require objects to use. (ex: the math class) In C++ I tried to do the same, but I can't create class functions that don't require a defined object. I usually define class Functions like so:

void myClass::myFunc{ //my func}

What am I missing?

Upvotes: 1

Views: 2154

Answers (3)

Piotr Styczyński
Piotr Styczyński

Reputation: 1426

There exist at least a couple of solutions for functions that are callable without any object that makes a context for them.

Static

Just like you do in Java you can use static keyword with any method inside C++ class.

Java

public class MyClass {
    public static void method() {
        doSomething();
    }
}

Call is done via: MyClass.method();

C++

class MyClass {
public:
    static void method() {
        doSomething();
    }
};

Call is done via: MyClass::method();

As you may know static allow us to make "context-free" methods (that do not require caller object).

Define static method outside class

Methods (not only static) can be defined outside C++ class

C++

class MyClass {
public:
    // Tell the compiler that we have method here
    // but do not provide any code
    static void method();
};

// Provide the code outside class
// NOTE: static keyword was omitted because we have specified
//       that the function is static earlier.
//       When you write the static there it won't compile.
//
void MyClass::method() {
    doSomething();
}

This code is equivalent to that shown previously.

Note the lack of static keyword near the void MyClass::method().

The C++ standard does not allow linkage specifiers (in this case it's our static keyword) to be placed near outside-class definitions.

And GCC compiler will shout at you when you do place static there:

 cannot declare member function 'static void MyClass::method()' to have static linkage

The details are a bit too deep for this answer, but the idea is that static inside class means something slightly different than outside the class.

You don't have to bother yourself why is that, just skip the static and be happy.

Functions in global scope

You can define the functions in global scope also.

This method is not applicable for Java as there is no such thing as global scope in Java, where everything is class-scoped.

C++

void callMe() {
    doSomething();
}

int main() {
    // The function callMe() is not defined in any class
    // so we call is directly 
    callMe();

    return 0;
}

Namespaces

You can extend global-scope functions approach to obtain namespaces abstraction.

The namespace can be understood as a group of classes/functions and other entities which names are prefixed with some defined name.

C++

namespace test {
    void callMe() {
        doSomething();
    }
}

Here we've got a function callMe() that is prefixed with name test.

This is an ordinary, plain function except the fact, that we call it using class-like notation:

C++

int main() {
    test::callMe();

    return 0;
}

Namespaces also have no equivalent in Java language.

The closest implementation is making a static function (again) inside class:

Java

public class Test {
    public static void callMe() {
        doSomething();
    }
}

Upvotes: 4

Christian Hackl
Christian Hackl

Reputation: 27518

In Java it's possible to have functions that don't require objects to use.

C++ is even better: you can have functions that don't even require classes to use, so-called non-member functions:

void myFunc() {
    // my func
}

The Java way, using a class with only static member functions, is also technically possible:

class MyClass {
public:
    static void myFunc() {
        // my func
    }
};

However, you rarely encounter such classes because they are not needed, due to the aforementioned fact that C++ supports non-member functions.

Upvotes: 2

Jake Freeman
Jake Freeman

Reputation: 1698

It is very simple just in your file do this:

void yay()
{
}

No need for classes though you could take the Java approach if you wanted.

Upvotes: 1

Related Questions