954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Static virtual functions - Reasons

Is having static virtual functions possible in C++ ?
I know its not as compiler throws an error but I am eager to know why?

the one reason I can think of (please validate)

"Virtual Functions are related to the object i,e calling a right functon on right object where as static functions scope is at the class level. So there is a wide gap between the two concepts hence it is not possible"

I know the reasoning above looks stupid but can you provide some solid grounds to it ?

ankitrastogi82
Newbie Poster
11 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Polymorphism occurs because of dynamic binding, which is made possible through use of pointers (The compiler internally represents a virtual function as a pointer, whose value is determined at run time) -

however, a static function is just that - its not a function pointer, but an object which has a static address.

If you want to call a derived class static function through a base class pointer, then you can use a non-static virtual function wrapper to achieve the same thing. eg,

#include <iostream>

class Base
{
    static void foo() { std::cout << "Base\n"; }
public:
    virtual void bar() { foo(); }

};

class Derived : public Base
{
    static void foo() { std::cout << "Derived\n"; }
public:
    virtual void bar() { foo(); }
};

int main()
{
    Base* obj = new Derived;
    obj->bar();
    delete obj;
}
Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

>Is having static virtual functions possible in C++ ?
No.

>I know the reasoning above looks stupid but can you provide some solid grounds to it ?
No, actually your reasoning is precisely what I would use to describe the restriction. The real answer is a little more involved though.

>however, a static function is just that - its not a function
>pointer, but an object which has a static address.
You're probably thinking of the usual way of accessing a static member function:

MyClass::my_static_function();

Expecting polymorphic behavior in this case is silly unless you use templates for compile-time polymorphism:

#include <iostream>

using namespace std;

class Foo {
public:
  static void bar() { cout<<"bar\n"; }
};

template <typename T>
void baz()
{
  T::bar();
}

int main()
{
  baz<Foo>();
}

But there's nothing in the language definition that prevents adding virtual static member functions. In fact, it would be rather simple to add the feature. The class calling mechanism ( MyClass::my_static_function(); ) would work the same and an extra rule could be added so that obj->my_static_function(); calls the most derived static member function.

virtual static member functions aren't presently supported in C++, but it's not because they're impossible or illogical. It's more likely to be an oversight followed by lack of demand.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You