static virtual function

D

Dumitru Sipos

Hello everybody!

is there possible to have a function that is both static and virtual?

Dumi.
 
N

Nicolas Pavlidis

Dumitru said:
Hello everybody!

is there possible to have a function that is both static and virtual?

No!
The reason is easy, virtual functions must be bound to an certain
object, but static functions are the same for all objects of a class.

A little hint:
Trye to code somthing like that and wou'll notice that the compiler
comlains about such code ;-)

Kind regards,
Nicolas
 
M

Mike Wahler

Dumitru Sipos said:
Hello everybody!

is there possible to have a function that is both static and virtual?

If you study the language definitions of what static member functions
and virtual member functions are, you should realize your question
does not make much sense.

-Mike
 
S

Sjoerd A. Schreuder

Mike said:
If you study the language definitions of what static member functions
and virtual member functions are, you should realize your question
does not make much sense.

It can make sense! RTTI typically doesn't depend on the this-pointer
(static) while RTTI typically depends on the dynamic type (virtual).

class Baseobject
{
virtual static std::string getCodeAuthor() = 0;
}

class A : BaseObject
{
virtual static std::string getCodeAuthor() { return "Sjoerd"; }
}

class B : BaseObject
{
virtual static std::string getCodeAuthor() { return "Mike"; }
}

Maybe there are reasons why "static virtuals" should not be allowed,
but "it doesn't make sense" is certainly not one of them.

Note: I'm quite aware of the fact that C++ doesn't allow
static virtuals, and of the fact that there is an easy work-around
by using regular virtual functions.

Sjoerd
 
R

Rolf Magnus

Sjoerd said:
It can make sense! RTTI typically doesn't depend on the this-pointer

Huh? Of course it depends on the this-pointer.
(static) while RTTI typically depends on the dynamic type (virtual).

.... of the object. But with a static function, you don't have an object.
class Baseobject
{
virtual static std::string getCodeAuthor() = 0;
}

class A : BaseObject
{
virtual static std::string getCodeAuthor() { return "Sjoerd"; }
}

class B : BaseObject
{
virtual static std::string getCodeAuthor() { return "Mike"; }
}

Maybe there are reasons why "static virtuals" should not be allowed,
but "it doesn't make sense" is certainly not one of them.

What would be the sense of the above? You would call the function e.g. as:

std::string author = Baseobject::getCodeAuthor();

Now which one should that call be dispatched to, and why?
 
H

Howard

Sjoerd A. Schreuder said:
It can make sense! RTTI typically doesn't depend on the this-pointer
(static) while RTTI typically depends on the dynamic type (virtual).

That's not correct. Calling a virtual function involves using the dynamic
(run-time) type of a specific object, which most certainly requires use of
the "this" poiinter. The "this" pointer is not static in any sense that I
know of, but rather is (or at least can be thought of as) a "hidden"
parameter to non-static member function calls.

You say RTTI depends on the dynamic type. Well, the dynamic type..of what?
It has to be the dynamic type of a specific object, and that information is
passed to the function call via the "this" pointer. A static function, on
the other hand, receives no such information, because it does not require a
specific object. So, how could it resolve the dynamic type if it doesn't
have an instance of the object from which to determine the correct type?

You show (below) some function definitions, but how would you call them
without a specific object? What information would be used to resolve which
override is to be called?
class Baseobject
{
virtual static std::string getCodeAuthor() = 0;
}

class A : BaseObject
{
virtual static std::string getCodeAuthor() { return "Sjoerd"; }
}

class B : BaseObject
{
virtual static std::string getCodeAuthor() { return "Mike"; }
}

Maybe there are reasons why "static virtuals" should not be allowed,
but "it doesn't make sense" is certainly not one of them.

It is the primary reason. A call to a static function does not include
reference to any specific object. A call to a virtual function does. The
two are incompatible.
Note: I'm quite aware of the fact that C++ doesn't allow
static virtuals, and of the fact that there is an easy work-around
by using regular virtual functions.

That's not a work-around for the problem, because there is no problem to
work around. Using "regular" virtual functions allows you to obtain correct
dynamic (polymorphic) behavior when using derived classes. Using static
functions allows you to call member functions without any object (instance)
of the specified class. Two different issues, two different solutions.

I'd be interested in hearing if there's any reason you'd even *want* to have
a static virtual function. Perhaps an example using the functions above...?

-Howard
 
S

Sjoerd A. Schreuder

Rolf said:
Huh? Of course it depends on the this-pointer.

Maybe this is more clear: "RTTI typically doesn't depend on the value
of the members"
... of the object. But with a static function, you don't have an object.

Why not? Static means no object during the function.

The following is possible:

class A {
public: static void f() {}
};

int main()
{
A a;
a.f(); // static function call
}

Change the declaration to static virtual void f() and there it is:
a static virtual function.

What would be the sense of the above? You would call the function e.g. as:

std::string author = Baseobject::getCodeAuthor();

I was thinking this:

std::string author = object.getCodeAuthor();
Now which one should that call be dispatched to, and why?

Your example explicitly calls Baseobject::getCodeAuthor(). Compare
that with object.A::f(), which is not dispatched virtually either
and will always call A::f().

My example would be dispatched depending on the dynamic type of object.

Your example shows why virtual static functions have a use!
Static functions only allow your example, virtual functions only
my example. Static virtuals allow both your example and my example.

Sjoerd

PS: I'm quite aware of the fact that there is an easy work-around
by using a regular virtual function that calls a regular static function.
 

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,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top