• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
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

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

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