| | |
Static virtual functions - Reasons
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 11
Reputation:
Solved Threads: 0
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 ?
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 ?
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,
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,
CPP Syntax (Toggle Plain Text)
#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; }
Last edited by Bench; Jul 16th, 2007 at 9:20 am.
¿umop apisdn upside down? >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:
Expecting polymorphic behavior in this case is silly unless you use templates for compile-time polymorphism:
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 (
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.
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:
C++ Syntax (Toggle Plain Text)
MyClass::my_static_function();
C++ Syntax (Toggle Plain Text)
#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>(); }
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.
New members chased away this month: 3
![]() |
Similar Threads
- Virtual functions (C++)
- Virtual functions, function objects, or what? (C)
- C++ derived class virtual functions (C++)
- performance benefit by not calling static member function by object (C)
- Dealing w/ virtual functions (C++)
Other Threads in the C++ Forum
- Previous Thread: problem with cygcurl-2.dll
- Next Thread: array error
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






