473,387 Members | 1,771 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,387 software developers and data experts.

Why are there no static virtual functions?

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

class Derived: public Base{
static std::string getName(){
return "Derived";
}
}

so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}

Could someone please explain why this is so?
Thanks Phil
Jan 17 '07 #1
15 3447
"Philipp" <si*******@freesurf.chwrote in message
news:11*************@sicinfo3.epfl.ch...
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";
}
}

class Derived: public Base{
static std::string getName(){
return "Derived";
}
}

so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}

Could someone please explain why this is so?
Thanks Phil
Why would you want to make it static? Just get rid of the static keyword,
and it does what you want.

What are you hoping to accomplish by adding the static keyword to the
method?
Jan 17 '07 #2
Philipp schrieb:
Hello
I don't exactly understand why there are no static virtual functions. I
Because virtual functions depend on the actual object you call the function
with. But static functions don't need objects.
would have liked something like this:
[...]
so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}
This can be done with typeid:

if (typeid(*pointer) == typeid(Derived))
// ...

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jan 17 '07 #3
Thomas J. Gritzan wrote:
Philipp schrieb:
>Hello
I don't exactly understand why there are no static virtual functions. I

Because virtual functions depend on the actual object you call the function
with. But static functions don't need objects.
I think of "virtual" as "gets overriden by derived classes", is this wrong?

In that sense, I was expecting that a static function could be overriden
by a derived class (ie: function is virtual _and_ static).

From your explaination I understand that this is a compiler /
optimization issue.

This can be done with typeid:

if (typeid(*pointer) == typeid(Derived))
// ...
Yes thanks (I did it with dynamic cast, same thing).

Phil
Jan 17 '07 #4
Philipp wrote:
I would have liked something like this:

class Base{
static virtual std::string getName(){
return "Base";
}
}

class Derived: public Base{
static std::string getName(){
return "Derived";
}
}

so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}
Virtual functions exist to eliminate this sort of explicit type
checking in your code.

See for instance

http://www.parashift.com/c++-faq-lit...e.html#faq-6.9

Cheers! --M

Jan 17 '07 #5
Philipp <si*******@freesurf.chwrote:
>Thomas J. Gritzan wrote:
>Philipp schrieb:
>>Hello
I don't exactly understand why there are no static virtual functions. I

Because virtual functions depend on the actual object you call the function
with. But static functions don't need objects.

I think of "virtual" as "gets overriden by derived classes", is this wrong?
Kind of. Non-virtual methods can be overridden also. If a method is
"virtual" then you are guaranteed that the correct method will be
called even if you access the object by a pointer to an ancestor
object and that ancestor has a different version of the method.

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Jan 17 '07 #6
Philipp wrote:
Thomas J. Gritzan wrote:
>Philipp schrieb:
>>Hello
I don't exactly understand why there are no static virtual functions. I

Because virtual functions depend on the actual object you call the
function with. But static functions don't need objects.

I think of "virtual" as "gets overriden by derived classes", is this
wrong?
That's right. The override that is chosen depends on the dynamic type of the
object that you call the virtual function for. But here's the trouble. A
static member function has no object, hence no dynamic type, so there is no
way to choose which function to actually call. Example:

#include <iostream>

class Base
{
public:
virtual void test() { std::cout << "Base\n"; }
};

class Derived1 : public Base
{
public:
virtual void test() { std::cout << "Derived1\n"; }
};

class Derived2 : public Base
{
public:
virtual void test() { std::cout << "Derived2\n"; }
};

int main()
{
Base b;
Derived1 d1;
Derived2 d2;

Base* p = &b;

p->test(); // Prints "Base"
p = &d1;
p->test(); // Prints "Derived1"
p = &d2;
p->test(); // Prints "Derived2"
}

Simple inheritance. Now with static virtuals (assuming they were allowed):

#include <iostream>

class Base
{
public:
static virtual void test() { std::cout << "Base\n"; }
};

class Derived1 : public Base
{
public:
static virtual void test() { std::cout << "Derived1\n"; }
};

class Derived2 : public Base
{
public:
static virtual void test() { std::cout << "Derived2\n"; }
};

int main()
{
Base::test(); // Prints Base
Derived1::test(); // Prints Derived1
Derived2::test(); // Prints Derived2
}

Now there is nothing dynamic anymore. The function to be called is chosen
statically. So what would the virtual keyword be good for? How and why
would the compiler at runtime choose which of those three functions to
call?
In that sense, I was expecting that a static function could be overriden
by a derived class (ie: function is virtual _and_ static).

From your explaination I understand that this is a compiler /
optimization issue.
No, it's a logic issue. Having a function that is both virtual and static
makes no sense.

Jan 17 '07 #7
Philipp wrote:
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";
}
}

class Derived: public Base{
static std::string getName(){
return "Derived";
}
}

so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}

Could someone please explain why this is so?
Thanks Phil
There is a logic hole in here. If you can have static virtual then you
can write:

class Base
{
public: static virtual void foo()
{
std::cout << "Base";
}
};

class Derived1: public Base
{
public: static void foo()
{
std::cout << "Derived1";
}
};

class Derived2: public Base
{
public: static void foo()
{
std::cout << "Derived2";
}
};

int main()
{
Base::foo(); // What's the output???
}

Ben
Jan 18 '07 #8
* benben:
Philipp wrote:
>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";
}
}

class Derived: public Base{
static std::string getName(){
return "Derived";
}
}

so I can compare with what some pointer gives me. eg.

Base* pointer = getSomeBasePointer();
if( pointer->getName == Derived::getName() ){
doSomething();
}

Could someone please explain why this is so?
Thanks Phil

There is a logic hole in here.
You mean, in the following:

If you can have static virtual then you
can write:

class Base
{
public: static virtual void foo()
{
std::cout << "Base";
}
};

class Derived1: public Base
{
public: static void foo()
{
std::cout << "Derived1";
}
};

class Derived2: public Base
{
public: static void foo()
{
std::cout << "Derived2";
}
};

int main()
{
Base::foo(); // What's the output???
}
It depends on how the hypothetical extension of C++ is defined.

There are two possibly useful definitions of "static virtual" I know of.
One is the OP's example, where a member function doesn't access the
'this' pointer implicitly or explicitly, and so can be called
statically, but where the point is that it can also be called
dynamically (virtually). This can be emulated by having an ordinary
virtual function forward to a corresponding static member function. The
other is to have "static virtual" denote a virtual member of a class'
meta-class. I believe this is supported in the Delphi language.

For the first definition the output would be "Base", since with that
definition the function is called statically in the example above, while
with the second definition it's more unclear what the output would be:
it would depend on the syntactical rules adopted (e.g. special syntax
for accessing the metaclass object, or not).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 18 '07 #9
Alf P. Steinbach wrote:
(...) where a member function doesn't access the
'this' pointer implicitly or explicitly, and so can be called
statically, but where the point is that it can also be called
dynamically (virtually). This can be emulated by having an ordinary
virtual function forward to a corresponding static member function.
Yes, thanks, on next refactoring I will do this.
Phil
Jan 18 '07 #10
Rolf Magnus wrote:
Philipp wrote:
>Thomas J. Gritzan wrote:
>>Philipp schrieb:
Hello
I don't exactly understand why there are no static virtual functions. I
Because virtual functions depend on the actual object you call the
function with. But static functions don't need objects.
I think of "virtual" as "gets overriden by derived classes", is this
wrong?

That's right. The override that is chosen depends on the dynamic type of the
object that you call the virtual function for. But here's the trouble. A
static member function has no object, hence no dynamic type, so there is no
way to choose which function to actually call. Example:

#include <iostream>

class Base
{
public:
virtual void test() { std::cout << "Base\n"; }
};

class Derived1 : public Base
{
public:
virtual void test() { std::cout << "Derived1\n"; }
};

class Derived2 : public Base
{
public:
virtual void test() { std::cout << "Derived2\n"; }
};

int main()
{
Base b;
Derived1 d1;
Derived2 d2;

Base* p = &b;

p->test(); // Prints "Base"
p = &d1;
p->test(); // Prints "Derived1"
p = &d2;
p->test(); // Prints "Derived2"
}

Simple inheritance. Now with static virtuals (assuming they were allowed):

#include <iostream>

class Base
{
public:
static virtual void test() { std::cout << "Base\n"; }
};

class Derived1 : public Base
{
public:
static virtual void test() { std::cout << "Derived1\n"; }
};

class Derived2 : public Base
{
public:
static virtual void test() { std::cout << "Derived2\n"; }
};

int main()
{
Base::test(); // Prints Base
Derived1::test(); // Prints Derived1
Derived2::test(); // Prints Derived2
}

Hello and thanks for your answer.
So what would the virtual keyword be good for?
So that you get dynamic binding on pointers, but still static access
with the classes name.
How and why
would the compiler at runtime choose which of those three functions to
call?
I think you provided my expected behavior in your example.
p->test() calls the derived (dynamic) test() for p*
Base::test() calls test() statically from Base
Derived1::test() calls test() statically from Derived1

As Mr. Steinbach pointed out, this can be achieved by the virtual
function calling a static function inside the class.
No, it's a logic issue. Having a function that is both virtual and static
makes no sense.
I still think it makes sense, but as there is an easy workaround, it
won't hurt me much.

Phil
Jan 18 '07 #11
Philipp wrote:
Hello
I don't exactly understand why there are no static virtual functions.
No, it's a logic issue. Having a function that is both virtual and static
makes no sense.

I still think it makes sense, but as there is an easy workaround, it
won't hurt me much.
How can you call your "static virtual" functions?
The keyword "virtual" means "use real class of real object" instead of
"use any base class of real object".

Are you going to call your "static virtual" function over pointer to
its base class without creating of object? But in the case your "static
virtual" function has no information about real class of object, has
only about class of pointer, because object is absent. You can call
only "base::foo" function, so the function can be ordinary static, not
virtual.

If you want to use already created object, you can declare your
function as ordinary virtual, not static.

If you think, that your virtual no need object after call, but C++ will
post to you hidden pointer to object, that it is compiler depended,
super-optimal compiler can generate virtuals without hidden pointers if
"this" is not used there, C++ does not deny, but not require.

Jan 19 '07 #12
On Wed, 17 Jan 2007 23:03:36 +0100, Rolf Magnus <ra******@t-online.dewrote:
Philipp wrote:
....
>I think of "virtual" as "gets overriden by derived classes", is this
wrong?

That's right.
No, it's incorrect, as explained in
Message-ID: <ja********************************@4ax.com>

BR,
/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jan 19 '07 #13
Jorgen Grahn wrote:
On Wed, 17 Jan 2007 23:03:36 +0100, Rolf Magnus <ra******@t-online.de>
wrote:
>Philipp wrote:
...
>>I think of "virtual" as "gets overriden by derived classes", is this
wrong?

That's right.

No, it's incorrect, as explained in
Message-ID: <ja********************************@4ax.com>
What's recursion again?
Anyway, it isn't explained there.

Jan 22 '07 #14
Jorgen Grahn wrote:
as explained in
Message-ID: <ja********************************@4ax.com>
Sorry, how can I view this message from the link you provide? (google
search didn't bring up anything). Thanks
Jan 29 '07 #15
Philipp wrote:
Jorgen Grahn wrote:
>as explained in
Message-ID: <ja********************************@4ax.com>

Sorry, how can I view this message from the link you provide? (google
search didn't bring up anything). Thanks
Search 'comp.lang.c++.*' for "no virtual static" and/or "no static
virtual" (with double quotes). A few good threads are found that way.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 29 '07 #16

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...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
4
by: Martin | last post by:
Greetings I want to have virtual member functionality, but without my member functions being virtual:-) As of yet this is all just in my head cause I can't see a nice solution yet so lets...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
14
by: vermarajeev | last post by:
Hi guys, I have problem understanding virtual functions??? Like how it works. My friend asked me some questions 1) What is a static function??? I replied functions to be used without...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
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 =...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.