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-18-2007
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
 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      01-19-2007
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.

 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-19-2007
On Wed, 17 Jan 2007 23:03:36 +0100, Rolf Magnus <(E-Mail Removed)> 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: <(E-Mail Removed)>

BR,
/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-22-2007
Jorgen Grahn wrote:

> On Wed, 17 Jan 2007 23:03:36 +0100, Rolf Magnus <(E-Mail Removed)>
> 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: <(E-Mail Removed)>


What's recursion again?
Anyway, it isn't explained there.

 
Reply With Quote
 
Philipp
Guest
Posts: n/a
 
      01-29-2007
Jorgen Grahn wrote:
> as explained in
> Message-ID: <(E-Mail Removed)>


Sorry, how can I view this message from the link you provide? (google
search didn't bring up anything). Thanks
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-29-2007
Philipp wrote:
> Jorgen Grahn wrote:
>> as explained in
>> Message-ID: <(E-Mail Removed)>

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


 
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