Alan Turing
Alan Turing

Reputation: 12571

C++ Overload Static Function with Non-Static Function

I would like to print two different things depending on whether a function is called statically with Foo::print() or from an instance of Foo foo; foo.print();

EDIT: Here is a class definition that definitely does not work, as answered by a few people already.

class Foo {
    string bla;
    Foo() { bla = "nonstatic"; }

    void print() { cout << bla << endl; }
    static void print() { cout << "static" << endl; }
};

However, is there a good way of achieving this effect? Basically, I would like to do:

if(this is a static call)
    do one thing
else
    do another thing

Phrased in another way, I know PHP can check if the *this variable is defined or not to determine whether the function is called statically. Does C++ have the same capability?

Upvotes: 46

Views: 34128

Answers (5)

SurajSinhIT
SurajSinhIT

Reputation: 1

1) In case of different return type of functions overloading is not possible in method overloading.

2) also if we pass same parameters in the functions define under the same class then it can not be overload.

Upvotes: 0

In silico
In silico

Reputation: 52149

No, it is directly prohibited by the standard:

ISO 14882:2003 C++ Standard 13.1/2 – Overloadable declarations

Certain function declarations cannot be overloaded:

  • Function declarations that differ only in the return type cannot be overloaded.
  • Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration (9.4).

...

[Example:

class X {
    static void f();
    void f();                // ill-formed
    void f() const;          // ill-formed
    void f() const volatile; // ill-formed
    void g();
    void g() const;          // OK: no static g
    void g() const volatile; // OK: no static g
};

—end example]

...

Besides, it would be ambiguous anyway since it's possible to call static functions on instances:

ISO 14882:2003 C++ Standard 9.4/2 – Static members

A static member s of class X may be referred to using the qualified-id expression X::s; it is not necessary to use the class member access syntax (5.2.5) to refer to a static member. A static member may be referred to using the class member access syntax, in which case the object-expression is evaluated. [Example:

class process {
public:
        static void reschedule();
}
process& g();
void f()
{
        process::reschedule(); // OK: no object necessary
        g().reschedule();      // g() is called
}

—end example]

...

So there would be ambiguity with what you have:

class Foo
{
public:
    string bla;
    Foo() { bla = "nonstatic"; }
    void print() { cout << bla << endl; }
    static void print() { cout << "static" << endl; }
};

int main()
{
    Foo f;
    // Call the static or non-static member function?
    // C++ standard 9.4/2 says that static member
    // functions are callable via this syntax. But
    // since there's also a non-static function named
    // "print()", it is ambiguous.
    f.print();
}

To address your question about whether you can check what instance a member function is being called on, there is the this keyword. The this keyword points to the object for which function was invoked. However, the this keyword will always point to an object i.e. it will never be NULL. Therefore it's not possible to check if a function is being called statically or not à la PHP.

ISO 14882:2003 C++ Standard 9.3.2/1 – The this pointer

In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called.

Upvotes: 68

Brian Roach
Brian Roach

Reputation: 76898

The answer is no, because you can't overload based on a return type.

You can certainly have static methods in a class, but you can't have:

static void foo();
void foo();

Because they have the same method signature.

EDIT: I saw your comment saying why you wanted to do this, and that you wanted to access member variables. You'd need to do this:

static void print(Foo f);
void print();
....
static void Foo::print(Foo f)
{
    int a = f.a;
    // do something with a
}

(Or create getters and setters in Foo, etc, but that's the general idea)

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283624

You can't do that exactly, see In silico's answer.

But you can make Foo::print() and Foo foo; print(foo); do different things. (Define void print(Foo& foo) in the same namespace as class Foo, it will be found by ADL).

In any case, this is not a good idea. You have two functions very similar in name which do completely different things, which violates good design principles.

Upvotes: 1

Janick Bernet
Janick Bernet

Reputation: 21184

It is definitely not allowed. I don't see any clean way of achieving this. What is exactly the problem that you want to solve this way?

Upvotes: 2

Related Questions