static member functions,

R

Rahul

Hi Everyone,

i was wondering why can't static member functions be made const or
volatile or virtual?

const static member function, not changing the values of static
member data
valatile static member function, indicating that volatile static
member could be changed beyond the program's control
virtual static member function, as the VTABLE is class specific...

Thanks in advance!!!
 
I

Ian Collins

Rahul said:
Hi Everyone,

i was wondering why can't static member functions be made const or
volatile or virtual?
Because they are not called on an instance of a class.
 
R

Rahul

Because they are not called on an instance of a class.

Yes i understand that the this pointer is not passed to these
functions, but at the same time, this pointer is not needed to read a
static member varaible (const static member function, just reading the
value of a variable and printing it on the screen)...
 
I

Ian Collins

*Please* don't quote signatures
Yes i understand that the this pointer is not passed to these
functions, but at the same time, this pointer is not needed to read a
static member varaible (const static member function, just reading the
value of a variable and printing it on the screen)...

A const member function is called on a const objects. There is no
equivalent for static members. For example,

struct X
{
void f();
void f() const;
};

int main()
{
X x;
const X* px = &x;

px->f(); // calls f() const
x.f(); // calls f()
}

How would the compiler know whether to call a const or normal static
member function?
 
A

Andrey Tarasevich

Rahul said:
i was wondering why can't static member functions be made const or
volatile or virtual?

const static member function, not changing the values of static
member data

Probably can be done that way. But const member functions in C++ are more than
just "can't change the data member values". They also participate in the
overloading mechanism, which can select the proper version of the function
(const or non-const) depending on the static type of the implicit 'this'
argument. Static member functions have no such argument, which means that their
proposed 'const' properties will be severely limited compared to non-static
member functions, probably not even worth bothering.
valatile static member function, indicating that volatile static
member could be changed beyond the program's control

Err... I can't make sense of what you said here. In order to tell compiler that
some data member (static or not) "could be changed beyond the program's control"
you simply declare that data member itself 'volatile'. 'volatile' on a
non-static method makes the whole object 'volatile' (from that method's point of
view) and also works with overload resolution. I.e. from the generic point of
view it is similar to 'const' on a method, but if you think about the specifics
of what 'volatile' actually does you should see that it just doesn't make much
sense with static member functions. If static data members are not 'volatile' by
themselves (were not declared as 'volatile' originally), what could be the point
of "temporarily" treating them as 'volatile' from within some particular static
method?
virtual static member function, as the VTABLE is class specific...

Virtual functions in C++ are functions, calls to which are resolved at run-time
based on the dynamic type of the object = on the dynamic type of '*this' value.
Static functions have no 'this' so there's no way to resolve their calls.

Of course, one can argue that virtual mechanism for virtual static member
functions should only be engaged when they are called with a non-qualified
member name "applied" to a concrete object (as opposed to an "objectless" call
with a fully-qualified name), but the usefulness of this, I believe, will be
more that limited. (Although this is a bit similar to how class-specific
'operator delete' behaves).
 
J

James Kanze

Because they are not called on an instance of a class.
[/QUOTE]
Yes i understand that the this pointer is not passed to these
functions, but at the same time, this pointer is not needed to read a
static member varaible (const static member function, just reading the
value of a variable and printing it on the screen)...

Because the type of the this pointer is the only thing the
cv_qualifiers of a function affect. (They also play a role in
overload resolution, of course, but this is easily handled.) The
fact that a function is const does *not* prevent it from
modifying static members of the class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top