By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,440 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,440 IT Pros & Developers. It's quick & easy.

static virtual function

P: n/a
Hello everybody!

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

Dumi.
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dumitru Sipos wrote:
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
Jul 22 '05 #2

P: n/a

"Dumitru Sipos" <du***********@gmail.com> wrote in message
news:1104930790.279965@slbhw0...
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
Jul 22 '05 #3

P: n/a
Mike Wahler wrote:
"Dumitru Sipos" <du***********@gmail.com> wrote in message
news:1104930790.279965@slbhw0...

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.


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

Jul 22 '05 #4

P: n/a
Sjoerd A. Schreuder wrote:
Mike Wahler wrote:
"Dumitru Sipos" <du***********@gmail.com> wrote in message
news:1104930790.279965@slbhw0...

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.


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?

Jul 22 '05 #5

P: n/a

"Sjoerd A. Schreuder" <sa**********@wanadoo.nl> wrote in message
news:41***********************@news.wanadoo.nl...
Mike Wahler wrote:
"Dumitru Sipos" <du***********@gmail.com> wrote in message
news:1104930790.279965@slbhw0...

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.


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
Jul 22 '05 #6

P: n/a
Rolf Magnus wrote:
Sjoerd A. Schreuder wrote:
Mike Wahler wrote:
"Dumitru Sipos" <du***********@gmail.com> wrote in message
news:1104930790.279965@slbhw0...

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.
It can make sense! RTTI typically doesn't depend on the this-pointer


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"
(static) while RTTI typically depends on the dynamic type (virtual).


... 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.

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();


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.

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.