access to a static function using Dot operator in C++

50 views Asked by At

I came across with a C++ code that is vague for me due to the way of access to static function member of a class. Basically, static function members must be accessed using scope resolution operator(::) but in the following code, access is allowed by dot(.) operator. I appreciate if enlighten me!

// Function Overloading
#include <iostream>

class calculator {
public:
    static int add(int a, int b) { return (a + b); }
    static double add(int a, double b) { return (a + b); }
    static int add(int a, int b, int c) { return (a + b + c); }
};

int main() {
    calculator c;
    std::cout << c.add(3, 3);
    std::cout << std::endl;
    std::cout << c.add(2, 3, 4);
    std::cout << std::endl;
    std::cout << c.add(2, 2.3);
    return 0;
}

I modified the code and tried to execute it with scope resolution operator. It works but if there is a necessity of creating an instance of the object, the static function is only accessible through '.' operator.

0

There are 0 answers