Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Emagen

Using static functions in a class

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

Whats the proper way to use static functions in a class? The following is the example that I have been playing around with to see how it static works, but can''t seem to get it to compile correctly. Any help is appreciated. class a { private: int i ; public: a () { i = 0 ; } ~a () {} static void inc () { i++ ; } } ; void main () { a class1 ; } Thanks

Share this post


Link to post
Share on other sites
Advertisement
Hey.

One thing to keep in mind is that static methods can only access static members of the class. Since static members are accessible through the class itself, a static method accessing an instance variable would have to know ''on what instance is this variable?''

hint: change i to static.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It seems you are declaring the static void inc() function in the header? I assume this is so you can also call it from outside the class. Thats fine to do but you can''t use the objects member variables inside it. You could always make the function "static void inc(a &aObject)". If you are really making a function to increment the int then why don''t you just make it a normal member function? If you don''t need to call this function from outside then you don''t need to declare it in the header.

One other thing...if this is really supposed to be for local use only the most correct thing is to put the function in an anonymous namespace.

So in the .cpp file you could go...

namespace
{
void inc( a &aObject )
{
a.i++;
}
}

...

Share this post


Link to post
Share on other sites
The keyword static is incredibly overloaded in C++. It has three different meanings that depend on where it is used in the code:

1. Static symbols declared outside of any function and class are local to the file, and persist for at least as long as main() is active.

2. Static symbols declared inside a function are local to the function, and persist from before the first call to the function to after the last call to the function.

3. Static symbols declared inside a class are local to the class and have only a single instance shared among all instances of the class.

I believe #3 is what you want but you are implementing #1 in your code.

Share this post


Link to post
Share on other sites
in your example, the private variable called "i" must also be static.
static member functions cannot change nonstatic member variables.

so a correct implementation would go something like so:


class Widget
{
public:
static void NewWidget()
{
count++;
}
int HowMany()
{
return count; // notice how this nonstatic function can still access "count" but cant change it.
}
private:
static int count;
};

int Widget::count = 0; // you must initialize all static variables like this. (preferably in the corresponding cpp file)


hope that clears some things up for you


-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!