Sign in to follow this  
Followers 0
noatom

Are static member functions ever used?

14 posts in this topic

Can someone give me an example about when that might come in handy?I can't figure anything! Why would one want a static member function? What problem could you solve just with that?

0

Share this post


Link to post
Share on other sites

If you need to pass a regular C-compatible function pointer to something (like a Windows Procedure, or a thread procedure) it has to be static so the calling conventions are the same.

 

Also if you have a member function that doesn't access any members of the class (except static ones) it should be static as well.

 

Class factories use static Create methods which act a bit like constructors as well (they create a new object and return the address).

 

Evil singletons use static methods to get the instance as well...

4

Share this post


Link to post
Share on other sites

Another case which I was just reviewing is differentiation of action.  Take for example the normalize function in a vector3 class:

 

class vector3
{
public:
   void     Normalize();  // Normalize "this" vector without return.  Folks will be annoyed..
   // or
   vector3 Normalize() const;  // Normalize the vector and return it, not modifying "this".
   // or a poorly written library could have the above and this next one at the same time:
   vector3& Normalize(); // Normalize "this" vector and return a reference.
   // Ack, same usage completely different meaning, which gets called?  Probably the compiler would bitch
   // since it doesn't know which to call in some cases.
};

 

So with a static function you could get both behaviors in an explicit manner:

 
class vector3
{
public:
   vector3&     Normalize();  // Normalize "this" vector.
   static vector3 Normalize( const vector3& );
};
 
From the signatures there is pretty much no way you can read those incorrectly or expect the behavior to be different than it is.  Not suggesting this is a great answer for a vector, I was just thinking about this recently as mentioned and this was a use/case.
1

Share this post


Link to post
Share on other sites

There is the static factory pattern. And my math library (and some utilities) are made out of static methods too.

 

A cross product isn't something inherent of an object. So my static math class deals with that (and a whole other bunch of operations).

Edited by TheChubu
1

Share this post


Link to post
Share on other sites
I'm not sure if its right but my input class requires static functions to do calls from a pointers. I have it set up so that persay W calls the function address stored and that just happens to be my static cameras class "move forward" function.
0

Share this post


Link to post
Share on other sites

Another case which I was just reviewing is differentiation of action.

Excellent example!

So with a static function you could get both behaviors in an explicit manner:

 
class vector3
{
public:
   vector3&     Normalize();  // Normalize "this" vector.
   static vector3 Normalize( const vector3& );
};
 
From the signatures there is pretty much no way you can read those incorrectly or expect the behavior to be different than it is.  Not suggesting this is a great answer for a vector, I was just thinking about this recently as mentioned and this was a use/case.


Some people try to distinguish them by their names as well. 'Normalize' (normalizes 'this'), 'Normalized' (returns a copy that is normalized). The benefit is that you see the difference when reading the function when in use, and not just when reading the function definition.
vecA.Normalize(); //Unknown whether this is returning or not.
vs:
Vector vecB = vecA.Normalized(); //Definitely returning.
Typically I just have my codebase always operate on 'this' and copy explicitely, but that's just by habit - not by consciously thinking it through - but I figure I'd throw out the naming difference as an idea that others sometimes use. I started a thread inquiring about this a year ago, which might be interesting.

Also note that with static vs non-static similarly named functions, you have to be aware of an unintuitive C++ feature: You can call static functions from a class instance.
myInstance.StaticFunction(27, "blah");
I always call my static functions using the class scope explicitly (even if called from within other class functions), as there is zero ambiguity in that method:
int result = MyClass::StaticFunction(27, "blah");
Edited by Servant of the Lord
1

Share this post


Link to post
Share on other sites

I generally use them for user callbacks:

 

 

 
class IWindow
{
public:
  //pass in ptr to object as we no longer have access to it directly
  static int OnMouse(int, int, int, IWindow*);
};
 
IWindow* w = new IWindow;
w->OnMouse = MyMouseProc;
Edited by irreversible
0

Share this post


Link to post
Share on other sites

If you want to implement sort of an auto increment type id (here called family):

 

#pragma once

struct BaseComponent
{
    typedef unsigned int Family;

protected:
    static Family family_count;
};

template <typename Derived>
struct Component : public BaseComponent {
    static Family family(void);
};

template<typename C>
unsigned int Component<C>::family(void) {
    static BaseComponent::Family Family = family_count++;
    return Family;
}

//"always" = if first called in that order
class Derived : public Component<Derived> {};
Derived::family(); // always 0
class Derived2 : public Component<Derived2> {};
Derived2::family(); //always 1 
Edited by The King2
0

Share this post


Link to post
Share on other sites

I generally use them for user callbacks:

 

 

 
class IWindow
{
public:
  //pass in ptr to object as we no longer have access to it directly
  static int OnMouse(int, int, int, IWindow*);
};
 
IWindow* w = new IWindow;
w->OnMouse = MyMouseProc;

 

 

That's not going to compile... you'd need to declare the functions pointer as

 

static int (*OnMouse)(int, int, int, IWindow*);

 

and you can use pointer to member functions for that anyway

 

If you want to implement sort of an auto increment type id (here called family):

 

#pragma once

struct BaseComponent
{
    typedef unsigned int Family;

protected:
    static Family family_count;
};

template <typename Derived>
struct Component : public BaseComponent {
    static Family family(void);
};

template<typename C>
unsigned int Component<C>::family(void) {
    static BaseComponent::Family Family = family_count++;
    return Family;
}

//"always" = if first called in that order
class Derived : public Component<Derived> {};
Derived::family(); // always 0
class Derived2 : public Component<Derived2> {};
Derived2::family(); //always 1 

And I'm not sure that will compile either since you can't call a function outside of a function body unless it is as an initializer for a global or static object... unless that's new in C++11

0

Share this post


Link to post
Share on other sites

And I'm not sure that will compile either since you can't call a function outside of a function body unless it is as an initializer for a global or static object... unless that's new in C++11

 

This does indeed compile, at least with the inofficial VS november 2012 compiler with extended c++11 support. Didn't know about that restriction though, but might really be c++11.

0

Share this post


Link to post
Share on other sites

And I'm not sure that will compile either since you can't call a function outside of a function body unless it is as an initializer for a global or static object... unless that's new in C++11

 

This does indeed compile, at least with the inofficial VS november 2012 compiler with extended c++11 support. Didn't know about that restriction though, but might really be c++11.

 

Doesn't compile in GCC:

http://ideone.com/AWMOUV

0

Share this post


Link to post
Share on other sites

Doesn't compile in GCC:

http://ideone.com/AWMOUV

 

I was talking about my code, the second half of paradigm shifter's post with the confusing "family" naming convections ;)

Edited by The King2
0

Share this post


Link to post
Share on other sites

If you declare global static object in c/cpp file "static type gObjectName;" you cannot link the object from the header file by calling "extern type gObjectName;".

0

Share this post


Link to post
Share on other sites

If you declare global static object in c/cpp file "static type gObjectName;" you cannot link the object from the header file by calling "extern type gObjectName;".

Good example of static, bad example of a static *member* function :)

I just wrote a static method in my Matrix class. The static identity() member function returns the identity matrix.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0