Interesting Use of Static Member Functions

Started by
9 comments, last by c2_0 18 years, 6 months ago
Hello all, I came across a use of static member functions that I am unfamiliar with and having a bit of a time understanding... In the example there were classes A, B, C such that: class A { public: static afunc(); } class B : public A { public: static afunc(); } class C : public B { public: } Then at some a point a call to C::afunc() was made. How is this legal given that C never declared a static afunc()? Or, how can class B have a static member function with the same name as class A given that the functions are not virtual? Which afunc() does class C inherit? Thanks Todd
Advertisement
class B would hide class A's function.
C would use B's function, i.e. the most derived function.

Now, virtual static functions just don't make any sense at all.

You could have virtual member functions that dispatch to the correct static functions though.
Static member functions and member data just mean that the contained is irrespective of the instantiated object. When you put a const in the definition of a member function, the compiler knows that said function shouldn't change the object's member data. With a static member function, the compiler knows that the function has nothing to do with the object's member data, or other functions.

So, that being said, through inheritance, the static keyword isn't what prevents a function from being inherited, the protected keyword does that. So, B inherited A's members, but defined it's own afunc. C inherited A's members, and got B::afunc in the process.
william bubel
Thanks guys.

It was B's function hiding A's that threw me.
I appreciate the help.

Todd
No problem
As a perhaps useless side note on static functions. You can also do this.

class A
{
public:
static afunc();
}

int main( int argc, char** argv[] )
{
A* pA = NULL;
pA->afunc();
return 0;
}

Notice how the poitner to class A is null. This code will still properly execute as most compilers are smart enough to see its a static mehtod and make the call directly.

Cheers
Chris
CheersChris
Even if it was a member function, it would still be "called directly"... the pointer has nothing to do with the function call, unless it's a virtual function.

For a non-static function though, the this pointer would be passed in (on x86 in ecx), however, as long as you're not accessing any data through it (normally that would be a static function :P), this would work fine with a null pointer as well.
Quote:Original post by c2_0
Even if it was a member function, it would still be "called directly"... the pointer has nothing to do with the function call, unless it's a virtual function.

For a non-static function though, the this pointer would be passed in (on x86 in ecx), however, as long as you're not accessing any data through it (normally that would be a static function :P), this would work fine with a null pointer as well.


I'm not sure you read my post, the whole point is that it works with a null pointer:). You go onto mention that it would work with a null pointer, which restated what I said:).

Cheers
Chris
CheersChris
Quote:Original post by chollida1
Quote:Original post by c2_0
Even if it was a member function, it would still be "called directly"... the pointer has nothing to do with the function call, unless it's a virtual function.

For a non-static function though, the this pointer would be passed in (on x86 in ecx), however, as long as you're not accessing any data through it (normally that would be a static function :P), this would work fine with a null pointer as well.


I'm not sure you read my post, the whole point is that it works with a null pointer:). You go onto mention that it would work with a null pointer, which restated what I said:).

Cheers
Chris


I read your post.

You just made it sound like it would work _because_ the function was marked static, and as if the pointer itself had anything to do with calling up a normal member function.
That is why it works because the function is static:) That's the only way it could work. If it was a non static virtual member function then it would crash trying to dereference the this pointer.

Hopefully that clears up your confusion??

Cheers
Chris
CheersChris

This topic is closed to new replies.

Advertisement