Jump to content
  • Advertisement
Sign in to follow this  
induster

c++ static methods

This topic is 3723 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

Can someone explain to me the reason for using a static method? Aren't all methods of a class static by defualt and shared among all instances of the class? TIA

Share this post


Link to post
Share on other sites
Advertisement
A static function doesn't need an instance of a class/struct to be called on. Example:


struct Foo
{
static void bar() {}
};

int main()
{
Foo::bar();
return 0;
}



One reason you'd use one is that a static method can access private and protected data members and functions, so you could use one for a callback for instance - the function is static, so doesn't need a valid this pointer, and can access all private members of any struct/class of the same type passed in.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
A static function doesn't need an instance of a class/struct to be called on. Example:

*** Source Snippet Removed ***

One reason you'd use one is that a static method can access private and protected data members and functions, so you could use one for a callback for instance - the function is static, so doesn't need a valid this pointer, and can access all private members of any struct/class of the same type passed in.


Of course, you should note the flip side of that: the function doesn't *have* a valid this pointer, so you need to pass in an instance in order to access any members.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Of course, you should note the flip side of that: the function doesn't *have* a valid this pointer, so you need to pass in an instance in order to access any members.
True. I should have been more clear.

An example of how such a callback would work:

// Callback function pointer
typedef void (*LPCallback)(int n, void* pUserData);

// Function that performs a callback:
void SomeFunction(LPCallback pfnCallback, void* pUserData)
{
for(int i=0; i<10; ++i)
pfnCallback(n, pUserData);
}

// Your code to call that:
struct Foo
{
static void Bar(int n, void* pUserData)
{
Foo* pThis = (Foo*)pUserData;
std::cout << pThis->strName << ": " << n << std::endl;
}

std::string strName;
};

// Example:
int main()
{
Foo foo;
foo.strName = "Blah";

SomeFunction(Foo::Bar, &foo);
return 0;
}

That sort of code frequently pops up when dealing with C interfaces (E.g. the Win32 API). If yuou're doing C++, functors or similar are a better idea.

Share this post


Link to post
Share on other sites
I often have a number of predicates for passing the STL algorithms as static functions in my classes. Example:


class Foo {
bool m_alive;

public:
static bool is_alive( const Foo& f ) { return f.m_alive; }
};

...
erase( remove_if( c.begin(), c.end(), Foo::is_valid ), c.end() );



Also useful for sorting and most of the other algorithms. Of course you could just use boost::lambda as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by UziMonkey
Of course you could just use boost::lambda as well.

boost::lambda's a bit of an ogre. For a situation like that, you could get away with std::mem_fun_ref; for more complicated situations, boost::mem_fn would suffice.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by UziMonkey
Of course you could just use boost::lambda as well.

boost::lambda's a bit of an ogre. For a situation like that, you could get away with std::mem_fun_ref; for more complicated situations, boost::mem_fn would suffice.


And let's not forget boost::bind.

Boost::bind augments the capabilities provided by the standard library functions std::bind1st and bind2nd. Typically, the syntax associated with functional composition programming can be quite daunting. Boost::bind functionality is exposed through a normalized syntax making it exceptionally easy to deal with function objects and functions. Its function adapters simplify the code you must write. You need not concern yourself with the usage of adapters such as ptr_fun, mem_fun_ref, and the like. This library adds functionality not achievable through the STL.

And regarding OP's question, I personally think use of static functions is popularized as a result of Object Oriented Programming making functions a big hairy taboo, even in hybrid languages where the functionality is still available (such as C++).

Share this post


Link to post
Share on other sites
I don't know what callbacks are yet. Does that basically mean "do something to object A if some event/condition happens elsewhere"...? That's a stab in the dark.

The posts were a little above my head because I don't know boost:: yet either. Are there cases in programming when you would never create instances of an object?

This leads me to another question about classes. Without creating an instance of a class, I noticed that I can use myClass().memberMethod() to change/use the data. How does it do that without creating a copy of the class (or does it)? Later if you create a new instance:

myClass instanceOfMyClass;

and then access stuff...

instanceOfMyClass.memberMethod();

the method will remain unchanged from how we manipulated it earlier with myClass().memberMethod()

I hope I'm communicating this clear enough.

Share this post


Link to post
Share on other sites
Quote:
Original post by induster
I don't know what callbacks are yet. Does that basically mean "do something to object A if some event/condition happens elsewhere"...? That's a stab in the dark.


"Do something. When you're done, call this callback." When this callback is invoked, you know some/particular action completed.

Quote:
Are there cases in programming when you would never create instances of an object?


Yes. When you don't have any internal state. Calculating cosine of a value, for example. You don't need an object, and there's no context.

Quote:
How does it do that without creating a copy of the class (or does it)? Later if you create a new instance:

myClass instanceOfMyClass;

and then access stuff...

instanceOfMyClass.memberMethod();


First declaration creates an instance. Just not on heap, but on stack. But the constructor gets called, and when you exit the scope (the "}" usually), destructor gets called and memory de-allocated.

Quote:
the method will remain unchanged from how we manipulated it earlier with myClass().memberMethod()


Methods don't change. That particular instance's members may be changed.


void foo()
{
SomeClass c; // c is constructed
c.changeValues(); // values of c are changed
}; // c is destroyed and no longer exists

foo(); // c is created and destroyed on each call to function
foo();
foo();

SomeClass c; // same c is changed 3 times
c.changeValues();
c.changeValues();
c.changeValues();

Share this post


Link to post
Share on other sites
Ah okay. What if we did this:

myClass().setX(5); // set member variable x to 5

and then do this...

myClass().getX(); // return the value of x

obviously the second call will return whatever the x default value is and not 5.

So, by calling a static method without creating an instance of myClass are we able to permanently change data in the class permanently? Or, do we use static on Accessor methods only?

by the way thanks for all the help.

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!