C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static virtual functions
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
kiran
Guest





PostPosted: Mon Dec 18, 2006 9:01 am    Post subject: static virtual functions Reply with quote



Why cannot a static member function of class be declared as virtual?

thanks,
Kiran


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Mon Dec 18, 2006 10:52 pm    Post subject: Re: static virtual functions Reply with quote



Static member functions can be called without an object (no this
pointer), but virtual functions dont make sense without an object.

/Patrik

kiran wrote:
Quote:
Why cannot a static member function of class be declared as virtual?

thanks,
Kiran



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ondra Holub
Guest





PostPosted: Mon Dec 18, 2006 11:05 pm    Post subject: Re: static virtual functions Reply with quote



kiran napsal:
Quote:
Why cannot a static member function of class be declared as virtual?

thanks,
Kiran


Because it may not be called as virtual function.

You can consider static class member function as global function with
changed visibility (it is simillar to namespace, but it may be private
or protected too). You do not need instance to call static member
function, so there is no virtual table available and virtual call
mechanism may not be used.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Nicola Musatti
Guest





PostPosted: Mon Dec 18, 2006 11:05 pm    Post subject: Re: static virtual functions Reply with quote

kiran wrote:
Quote:
Why cannot a static member function of class be declared as virtual?

Because in order to dispatch a virtual member function you need an
instance of the class, whose dynamic type determines the choice of
function to call. No such instance is available for a static member
function.

On the other hand you can always define a non static, virtual member
function and not refer to any non static member in its implementation.

Cheers,
Nicola Musatti


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ron Natalie
Guest





PostPosted: Mon Dec 18, 2006 11:06 pm    Post subject: Re: static virtual functions Reply with quote

kiran wrote:
Quote:
Why cannot a static member function of class be declared as virtual?

What would it mean if it were? Static members aren't associated with

any particular object, there's no object type (static or dyanmic) to
determine the overrider.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Arun
Guest





PostPosted: Mon Dec 18, 2006 11:11 pm    Post subject: Re: static virtual functions Reply with quote

kiran wrote:
Quote:
Why cannot a static member function of class be declared as virtual?


Function resolution (mapping a function call to its definition) for a
virtual function is dynamic(Happens at run time with the help of vptr
and vtable).
Where as static function resolution happens at compile time. At compile
time itself all call references to a static function will be resolved
by the compiler.

This is the reason why we can not have (even conceptually) a static
member function, virtual.

Note that the static member function is nothing but a static function
with a specified class scope.

We do not need objects to call a static function, where as a virtual
function works only with objects.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dino
Guest





PostPosted: Mon Dec 18, 2006 11:12 pm    Post subject: Re: static virtual functions Reply with quote

Then it wouldn't be static. You could have a templated static.

Cheers,
D.


kiran wrote:
Quote:
Why cannot a static member function of class be declared as virtual?

thanks,
Kiran



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Tue Dec 19, 2006 1:44 am    Post subject: Re: static virtual functions Reply with quote

Bob a icrit :

Quote:
kiran wrote:

Why cannot a static member function of class be declared as virtual?


This question is probably a candidate for the C++ FAQ, if it's not
already thre.

A virtual member function is invoked in context of an object. For
example;

object->virtual_function();

where the actual function called depends on the actual type
of object.

A static member function is invoked without regard to any object,
so the actual function called cannot depend on the type of an
object.

So the two concepts (virtual and static member functions) are
mututally exclusive.


Well, Object Pascal has had the moral equivalent of virtual static
members for more than a decade now. They come in very handy
to implement factories or other kind of metaclass information.

I am not claiming that C++ needs them.
I am claiming that the concept makes a lot of sense and is not
at all inconsistent nor even useless ...

--- Raoul

Ref: See Class methods at :
http://info.borland.com/techpubs/delphi/delphi5/oplg/classes.html


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
John G Harris
Guest





PostPosted: Tue Dec 19, 2006 3:25 am    Post subject: Re: static virtual functions Reply with quote

In message <memo.20061218121050.248B (AT) brangdon (DOT) cix.compulink.co.uk>, Dave
Harris <brangdon (AT) cix (DOT) co.uk> writes
Quote:
kiran.tangeeda (AT) gmail (DOT) com (kiran) wrote (abridged):
Why cannot a static member function of class be declared as virtual?

Because classes are not reified as run-time objects, which means static
member functions don't have a "this" or a vtable to keep track of the
dynamic type of the class.

Which in turn is done for variety and efficiency. It didn't have to be
that way, and in other languages (eg Smalltalk) the equivalent of static
member functions are virtual, and jolly useful they are too. Smalltalk
comes with a full meta-class system. If you want that in C++ you can build
the meta-classes yourself.
snip


Why stop at classes ? If you could have a reference to complex<double>
then the next thing to ask for is a reference to complex :-)

John
--
John Harris

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bob
Guest





PostPosted: Tue Dec 19, 2006 10:10 am    Post subject: Re: static virtual functions Reply with quote

hasta_l3 (AT) hotmail (DOT) com wrote:


Quote:
Well, Object Pascal has had the moral equivalent of virtual static
members for more than a decade now. They come in very handy
to implement factories or other kind of metaclass information.

I am not claiming that C++ needs them.
I am claiming that the concept makes a lot of sense and is not
at all inconsistent nor even useless ...


Class methods are the Object Pascal equivalent of C++ static
member functions.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Nicola Musatti
Guest





PostPosted: Wed Dec 20, 2006 12:04 am    Post subject: Re: static virtual functions Reply with quote

hasta_l3 (AT) hotmail (DOT) com wrote:
Quote:
Bob a écrit :
hasta_l3 (AT) hotmail (DOT) com wrote:

Well, Object Pascal has had the moral equivalent of virtual static
members for more than a decade now. They come in very handy
to implement factories or other kind of metaclass information.
[...]
Class methods are the Object Pascal equivalent of C++ static
member functions.

and they can be virtual...

The implicit this (aka Self) argument is - sort of - a reference to
a metaclass.

That's precisely the point: in order to implement static virtual member
functions you need an explicit representation of classes as objects,
which C++ does not provide.

Cheers,
Nicola Musatti


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Kanze
Guest





PostPosted: Wed Dec 20, 2006 12:20 am    Post subject: Re: static virtual functions Reply with quote

Dave Harris wrote:
Quote:
kiran.tangeeda (AT) gmail (DOT) com (kiran) wrote (abridged):
Why cannot a static member function of class be declared as virtual?

Because classes are not reified as run-time objects, which
means static member functions don't have a "this" or a vtable
to keep track of the dynamic type of the class.

That's not quite true. What does typeid( MyClass ) give you, if
it isn't a concrete run-time object associated with the class.

Quote:
Which in turn is done for variety and efficiency. It didn't have to be
that way, and in other languages (eg Smalltalk) the equivalent of static
member functions are virtual, and jolly useful they are too. Smalltalk
comes with a full meta-class system. If you want that in C++ you can build
the meta-classes yourself.

It would probably be better to extend type_info.

Quote:
In other words, for every class MyClass, you add a (meta) class
MyClass_Class, and create an instance of the (meta) class as static member
of the class called class_. Where class A inherits from class B,
(meta) class A_Class inherits from B_Class. Instead of A::function() you
have A::class_.function(). That calls a virtual function of A_Class on the
static member of A, and it all works normally. (I put "meta" in brackets
here because C++ doesn't have real meta-classes, but that is what you are
modelling.)

I'm not sure what this buys us compared to something like the
Java approach, where there is just one type Class (a very
extended version of C++'s type_info).

Quote:
The fact that you can build a structure like this by hand when you need
it, is an argument for the language not forcing it upon you when you don't
need it.

Building the structure isn't the problem; using it is.

With regards to static virtual functions, of course, you don't
need it. A static function can be called in two ways:

MyClass::staticFunc() ;
ptrObj->staticFunc() ;

The first way, you get the class member---no virtual resolution
(and you wouldn't get virtual resolution even if the function
were non-static), and in the second, you have an object, so
there's no problem using virtual resolution, even if you don't
pass the this pointer to the function.

The question is whether it is really useful. It still doesn't
allow being virtual without having an object.

--
James Kanze (GABI Software) email:james.kanze (AT) gmail (DOT) com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Wed Dec 20, 2006 3:45 am    Post subject: Re: static virtual functions Reply with quote

Nicola Musatti a écrit :
Quote:
hasta_l3 (AT) hotmail (DOT) com wrote:
Bob a écrit :
hasta_l3 (AT) hotmail (DOT) com wrote:

Well, Object Pascal has had the moral equivalent of virtual static
members for more than a decade now. They come in very handy
to implement factories or other kind of metaclass information.
[...]
Class methods are the Object Pascal equivalent of C++ static
member functions.

and they can be virtual...

The implicit this (aka Self) argument is - sort of - a reference to
a metaclass.

That's precisely the point: in order to implement static virtual member
functions you need an explicit representation of classes as objects,
which C++ does not provide.

Well, Object Pascal doesn't exactly represent classes as objects.
They don't have data member, for example. And some kind of metaclass
information already made its way into C++ under the disguise of
type_info.

Nevertheless, I dont think that C++ should ever have virtual static
functions.
For most of their power come from virtual constructor functions, and
*that*
concept is pretty foreign to C++.

My point in this thread was to stress that the answer to the FAQ raised

is not that virtual static functions are meaningless or useless.

It is that they don't mix well with a few C++ key design decisions...

Regards, Nicola

--- Raoul


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Wed Dec 20, 2006 3:46 am    Post subject: Re: static virtual functions Reply with quote

Ron Natalie a écrit :
Quote:
hasta_l3 (AT) hotmail (DOT) com wrote:

Class methods are the Object Pascal equivalent of C++ static
member functions.


and they can be virtual...

The implicit this (aka Self) argument is - sort of - a reference to
a metaclass.


You still haven't indicated where this "class identity" comes from
where it would be different from the class the method is defined in?


In a nutshell : (I dont want to make the thread too much off-topic :-)

If one has a type T that happens to be a class,
then "class of T" is the type of the meta-class.

The identifier T actually refers to the meta-class
(and one can call a class method F with the syntax T.F)

One can have variables of type "class of T" holding
references to the metaclass, initialized from the value
of the identifier T.

If T2 derives from T, then one can have a method T2.F
which overrides T.F, and have variables of type "class of T2".

There is an implicit conversion from "class of T2" to "class of T",
and one may therefore have a variable v with static type "class of T"
but dynamic type "class of T2.

The expression v.F will then call T2.F

Have a nice day, Ron

--- Raoul


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dave Harris
Guest





PostPosted: Wed Dec 20, 2006 3:48 am    Post subject: Re: static virtual functions Reply with quote

james.kanze (AT) gmail (DOT) com (James Kanze) wrote (abridged):
Quote:
Because classes are not reified as run-time objects, which
means static member functions don't have a "this" or a vtable
to keep track of the dynamic type of the class.

That's not quite true. What does typeid( MyClass ) give you, if
it isn't a concrete run-time object associated with the class.

Yes, but all those objects have the same type, so don't help us here.


Quote:
It would probably be better to extend type_info.

I don't see how that would help.


Quote:
I'm not sure what this buys us compared to something like the
Java approach, where there is just one type Class (a very
extended version of C++'s type_info).

It buys us virtual static member functions. Static functions are not
virtual in Java, partly because they failed to provide a full meta-class
system. (Which I think is a shame, incidently. They missed a trick there.)

Perhaps a more elaborate example would help?

Given:
struct A {
static virtual int func() { return 1; }
static int test() { return func(); }
};

struct B : A {
static virtual int func() { return 2; }
}

int x = B::test();

the compiler would produce something like:

struct A {
struct __Class {
virtual int func() { return 1; }
int test() { return func(); }
};
static __Class __class;
};

struct B : A {
struct __Class : A::__Class {
virtual int func() { return 2; }
};
static __Class __class;
};

int x = B::__class.test();

Notice that A::__class and B::__class are not just different objects, they
have different types, too. This allows them to have different behaviour.


Quote:
A static function can be called in two ways:

MyClass::staticFunc() ;
ptrObj->staticFunc() ;

These would be equivalent. In both cases, the object used would be
MyClass::__class, not *ptrObj.


Quote:
The first way, you get the class member---no virtual resolution

That misses the point. We want to get virtual resolution despite not
having supplied an object explicitly. If we can supply an object we might
as well just use virtual functions on that.

-- Dave Harris, Nottingham, UK.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.