Sign in to follow this  
Jouei

Getting Around Static Member Function Problem

Recommended Posts

Hello all again, I have a class writen up and it has a static function and for what im using it for it has to be static! heres a small example to try and illustrate my problem struct s1 { static void func(); int i; }; void s1::func() { i = 1; // Error refrence to non static class member illegal s1 Test; Test.i = 1; // This dose not actualy change the vaule of i just the vaule of //Test.i } void main() { s1 Test; Test.i = 200; Test.func(); cout <<Test.i; cin.get(); } This is based of the example of Msdn they sead in order to get around the refrence member problem You have to create an instance to the class however with there example and many other that does not actualy change the value of the class that runs the function! so by running this example the output will still be 200 instead of the required 1 witch iv been trying to get out of it! Thanks to all of you ahead of time

Share this post


Link to post
Share on other sites
Hope this was what you were looking for.


#include <string>
#include <iostream>

using namespace std;

struct s1 {
static void func();
static int i; // **SOLUTION** for a static member function to modify any member data
// it too must be also be static.
};

int s1::i = 0; // **SOLUTION** static types of an object must also be initialized as such.

void s1::func()
{
i = 1; // Error refrence to non static class member illegal
s1 Test;
Test.i = 1; // This dose not actualy change the vaule of i just the vaule of
//Test.i
}

void main()
{
s1 Test;
Test.i = 200;
Test.func();
cout <<Test.i;
cin.get();
}


Share this post


Link to post
Share on other sites
That works, but its not correct. Create two instances of s1, call func() on instance 1, then print i from instance 2. What do you see?

The OP seems to want an instance of i for each instance of s1. Declaring i as static means that there is only a single instance of i which is shared between every instance of s1.

A static method cannot access any non-static members of the class. Period. Non-static members are the data which exists per-object. In the plainest possible terms, a static function can not access per-object data unless, of course, it has a reference (or pointer) to the object in question.

So, here are your options:
One method would be to pass a reference or pointer to Test into func(), however, that also requires that the instance of Test must exist before calling upon the static method -- which I expect is the problem, or you wouldn't be jumping through these hoops in the first place.

So what you really need to do is to make it so that func() doesn't need to be static. What you want to do isn't possible with a static method, and it indicates that whatever portion of your program that requires the static method has design problems.


Give some more information about your specific problem and, most importantly, why you believe that a static method is required to solve it.

Also, you need to educate yourself more about what "static" means in the context of a class for both methods and members because I can see you are confused.

One point of confusion I can see clearly is that you've used "Test1.func()" to call on the function, which is merely syntactic sugar and which I personally consider to be a useless bain on newbie programmers since it presents an unrealistic picture of what's happening. The "proper" way to access static methods of s1 looks like "s1::func()" As you can easily see here, func() has no idea of any specific instance of s1, only that it is part of the s1 class. Static methods are simply functions which have access to the static data of a class, much the same way that non-static methods are simply functions that have access to non-static members (again, syntactic sugar rears its ugly head; if i is declared static, it should be thought of as s1::i, rather than Test1.i, even though the latter form is legal).

[Edited by - ravyne2001 on May 23, 2007 4:56:47 AM]

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