942,957 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 33969
  • C++ RSS
Jul 16th, 2007
0

Static virtual functions - Reasons

Expand Post »
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 ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ankitrastogi82 is offline Offline
11 posts
since Aug 2005
Jul 16th, 2007
1

Re: Static virtual functions - Reasons

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,

CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jul 16th, 2007
0

Re: Static virtual functions - Reasons

>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:
C++ Syntax (Toggle Plain Text)
  1. MyClass::my_static_function();
Expecting polymorphic behavior in this case is silly unless you use templates for compile-time polymorphism:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem with cygcurl-2.dll
Next Thread in C++ Forum Timeline: array error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC