Jump to content
  • Advertisement
Sign in to follow this  
tscott1213

Interesting Use of Static Member Functions

This topic is 4655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!