Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why are there no static virtual functions?

Reply
Thread Tools

Why are there no static virtual functions?

 
 
Philipp
Guest
Posts: n/a
 
      01-17-2007
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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-17-2007
"Philipp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?


 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      01-17-2007
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
 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      01-17-2007
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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-17-2007
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

 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      01-17-2007
Philipp <(E-Mail Removed)> 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?


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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-17-2007
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.

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      01-18-2007
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-18-2007
* 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?
 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      01-18-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why not virtual static method Tony Johansson C++ 2 08-21-2005 01:13 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
If declared as virtual in base, its derived version also is virtual. Why not for destructors? qazmlp C++ 7 07-27-2004 03:10 PM



Advertisments