473,382 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

static virtual function

Hello everybody!

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

Dumi.
Jul 22 '05 #1
6 5799
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 =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.