Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 627
Alan Turing Avatar asked Mar 19 '11 23:03

Alan Turing


People also ask

Can we overload static and non-static method together?

The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters.

Can a static function call a non-static function in C?

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one. Create an object of the class inside the static method and then call the non-static method using such an object.

Can static member functions 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).

Can we use non-static method in method overloading?

Yes they can overload each other.


1 Answers

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.

like image 84
In silico Avatar answered Oct 14 '22 11:10

In silico