virtual static members.

Started by
22 comments, last by Promiscuous Robot 23 years, 2 months ago
Basically I am just wondering if its possible, and if it''s something to be avoided, or has issues, or something.
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Advertisement
I don''t think it''s possible..

-Marc
-SiliconReality Software
-http://www.blackenfall.org
-Marc-SiliconReality Software-http://www.blackenfall.org
quote:Original post by Promiscuous Robot
Basically I am just wondering if its possible, and if it''s something to be avoided, or has issues, or something.


Not possible. static methods run at class-level, while virtual methods run at object-level.



---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
The easiest way to tell if it is possible is to try it and see if the compiler accepts it. I don''t see any technical reason why you couldn''t have virtual static methods. The same arguement presented would be just as valid for arguing you couldn''t have static methods at all. The only issue that really matters though is whether the compiler will flag it as an error or not.
Keys to success: Ability, ambition and opportunity.
Is this C++ you''re talking about? If so "virtual static" doesn''t even make sense. static members have no object associated with them - they''re basically no different than a regular (non-member) function except that they''re in a different namespace. "virtual" is inherently concerned with the object heirarchy. No object, no virtual.

What are you *really* trying to do that you think you need "virtual static"?

-Mike
i agree w/ lilbuddywizer, because what to say that you can't override a static function in a derived class?

I think it's perfectly legal, and u know what?

*adds virtual to a static member*
*hits compile*

well, msvc++ doesn't allow it, but hey, msvc isn't ansi.


Compiler Error C2576
'identifier' : virtual used for static member function


A static member function was declared as virtual.

The virtual function mechanism relies on the specific object that calls the function to determine which virtual function is used. Since this is not possible for static functions, they cannot be declared as virtual.


oh well...



Edited by - Succinct on February 1, 2001 6:12:08 PM
-- Succinct(Don't listen to me)
Think about it.
A virtual static is an oxymoron.

You /can/ override static methods, but they cannot be called from a base class pointer. Static methods are not _thiscall's, so they don't get a vtable, so they can't be virtual because there's no way to resolve a virtual call without a this pointer.

Remember, you can call a static methods just using the class name, you don't need any pointer at all.

If you can determin which static method to use without using the this pointer, then there's no reason to make it virtual.
QED

Edited by - Magmai Kai Holmlor on February 1, 2001 6:25:50 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Hmm.. yes yes I realize the stupidity of the request.. But I asked because it would''ve made my life easier..

Oddly enough I don''t even remember why I wanted one because I guess I designed around it...

oh well.
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
I tried too

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by Succinct
i agree w/ lilbuddywizer, because what to say that you can''t override a static function in a derived class?

I think it''s perfectly legal, and u know what?


Yup, you''re wrong. The ANSI C++ Standard and the nature of the qualifiers ''static'' and ''virtual'' dictate that they cannot be used together.

Depending on your compiler to define what is legal will cause no end of problems when you try to port your software, use another compiler, or the compiler changes (and yes, the latter happens regularly).


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

This topic is closed to new replies.

Advertisement