Static virtual functions - Reasons

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 11
Reputation: ankitrastogi82 is an unknown quantity at this point 
Solved Threads: 0
ankitrastogi82 ankitrastogi82 is offline Offline
Newbie Poster

Static virtual functions - Reasons

 
0
  #1
Jul 16th, 2007
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 ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Static virtual functions - Reasons

 
0
  #2
Jul 16th, 2007
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,

  1. #include <iostream>
  2.  
  3. class Base
  4. {
  5. static void foo() { std::cout << "Base\n"; }
  6. public:
  7. virtual void bar() { foo(); }
  8.  
  9. };
  10.  
  11. class Derived : public Base
  12. {
  13. static void foo() { std::cout << "Derived\n"; }
  14. public:
  15. virtual void bar() { foo(); }
  16. };
  17.  
  18. int main()
  19. {
  20. Base* obj = new Derived;
  21. obj->bar();
  22. delete obj;
  23. }
Last edited by Bench; Jul 16th, 2007 at 9:20 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Static virtual functions - Reasons

 
0
  #3
Jul 16th, 2007
>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:
  1. MyClass::my_static_function();
Expecting polymorphic behavior in this case is silly unless you use templates for compile-time polymorphism:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Foo {
  6. public:
  7. static void bar() { cout<<"bar\n"; }
  8. };
  9.  
  10. template <typename T>
  11. void baz()
  12. {
  13. T::bar();
  14. }
  15.  
  16. int main()
  17. {
  18. baz<Foo>();
  19. }
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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC