static member function

T

Tony Johansson

Hello experts!

Why is not possible to have virtual static members

Many thnakn

//Tony
 
R

Raghavendra Mahuli

Hello,
virtual itself implies dynamic binding....
how can it be mixed with static??
 
G

Greg

Tony said:
Hello experts!

Why is not possible to have virtual static members

Many thnakn

//Tony

Can you describe what a virtual static function would mean?

I suspect that one reason that C++ has been reluctant to add support
for virtual static functions, is that no one seems to know what one is.

Greg
 
R

Robben

Tony said:
Hello experts!

Why is not possible to have virtual static members

Many thnakn

//Tony
From your postings over the period of time, I find that you are on a
good mission to explore c++. I appreciate your eagerness but I would
advise you to find if a similar query has already been posted.
Try seaching on "static virtual" on either comp.lang.c++ or
comp.lang.c++.moderated, you will get very good information.

I searched through the list and found quite good information regarding
your query.


Best of luck.

Thank you.
 
G

Girish Shetty

Its because,
Static means, with out creating object of that class, you can acccess it. It
means, it will be resolved during compilation time.
Where in, virtual functions, means, there will be virtual table creation,
virtual pointer assignment mechanism and that function call will be
determined during runtime( if we use, either, pointer or reference of base
ptr to access). So, this goes with run-time.

These two operations are totally going in opposite direction. So, its not
allowed and more over it does not make any valid sense.

Lets take one more example, virtual inline functions. We all know that
inline is just a request to the compiler. And if we call inline function
which is virtual, using pointer or reference of Base, then inline request
will be rejected and virtual comes in to picture.

But where in, static and virtual, both are not request.. so, its not
allowed.

Regards
Girish
 
J

Jay Nabonne

Can you describe what a virtual static function would mean?

I suspect that one reason that C++ has been reluctant to add support
for virtual static functions, is that no one seems to know what one is.
(The following are my definitions. They probably diverge from the standard
in the details.)

### Function requirements: "member" vs. "static"

A normal member function is one that requires an object *within the body
of the function*. It can be considered an "object level" function.

A static function is one that does not have an object instance passed into
it. It could be considered a "class level" function.

### Dispatch: "compile-time" vs. "run-time" binding

A call to a function can be bound at compile time ("statically" (ugh!)) or
at run time (dynamically). The latter is what virtual functions give you.

These above concepts are orthogonal to each other. You get four
combinations:

1) Object member function bound at compile time ("normal" object function
call: o->fun()).

2) Static function bound at compile time (ClassName::fun())

3) Object member function bound at run time ("virtual" function call:
o->vfun()

4) Static function bound at run time through an object instance (o->svfun())

Case number 4 is "virtual static" and requires an object to bind the
function, but the function call itself is "static", i.e. the function does
not require an object within its body and semantically is not an object
member function. You just use the object to dispatch to the right static
function.

I have wanted this in the past for dynamically invoking class-level
functions that are also invoked elsewhere without an object (static
binding). I usually end up with a static function for static binding *and*
a virtual function calling it for dynamic dispatch, but then I need to use
some less-than-ideal naming since they can't be called the same thing in
the class.

To the OP:

The best argument I've heard why this feature doesn't work is that if you
have a "vs" function then try to call *another* "vs" function
from within the first that you call, there is no object to dispatch with.
The chain is broken. Once you look at it from that perspective, you
realize a first-level call would work, but anything after that would be
limited enough to break normal C++ semantics (in my opinion).

- Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top