473,465 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static virtual function

Hello everybody!

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

Dumi.
Jul 22 '05 #1
6 5810
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

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
3
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
15
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
2
by: Markus Dehmann | last post by:
I need a simple object serialization, where loading an object from file looks like this: Foo* foo1 = FooFactory::create("./saved/foo1.a321f23d"); Foo* foo2 =...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.