static functions and non static variables 
Author Message
 static functions and non static variables

Hello All,

I have a little problem that i hope some of you might be able to help me
with.

Basically i have a class which contains a function declared as static:

// MyClass.h
class CMyClass
{
public:
// PUBLIC FUNCTIONS
        static BOOL MyStaticFunction(void);
// PUBLIC VARIABLES
        BOOL m_bDone;

Quote:
};

But the problem is i need to change the value of m_bDone in the function
MyClass::MyStaticFunction(). Like this ...

BOOL CMyClass::MyStaticFunction(void)
{
        m_bDone = FALSE;                // This generates a compiler error.
        CMyClass::m_bDone = FALSE;      // This generates a compiler error.

Quote:
}

If anyone knows a way around this problem, please get back to me either

Thanks in advance,

Conner.



Sun, 31 Dec 2000 03:00:00 GMT  
 static functions and non static variables
Non static members are associated with particular instance of an object of
the class.

So there is no meaning in changing non-static member variables in static
functions.

Quote:

>Hello All,

>I have a little problem that i hope some of you might be able to help me
>with.

>Basically i have a class which contains a function declared as static:

>// MyClass.h
>class CMyClass
>{
>public:
>// PUBLIC FUNCTIONS
> static BOOL MyStaticFunction(void);
>// PUBLIC VARIABLES
> BOOL m_bDone;
>};

>But the problem is i need to change the value of m_bDone in the function
>MyClass::MyStaticFunction(). Like this ...

>BOOL CMyClass::MyStaticFunction(void)
>{
> m_bDone = FALSE; // This generates a compiler error.
> CMyClass::m_bDone = FALSE; // This generates a compiler error.
>}

>If anyone knows a way around this problem, please get back to me either

>Thanks in advance,

>Conner.



Sun, 31 Dec 2000 03:00:00 GMT  
 static functions and non static variables


Quote:
>Hello All,

>I have a little problem that i hope some of you might be able to help me
>with.

>Basically i have a class which contains a function declared as static:

>// MyClass.h
>class CMyClass
>{
>public:
>// PUBLIC FUNCTIONS
>    static BOOL MyStaticFunction(void);
>// PUBLIC VARIABLES
>    BOOL m_bDone;
>};

>But the problem is i need to change the value of m_bDone in the function
>MyClass::MyStaticFunction(). Like this ...

>BOOL CMyClass::MyStaticFunction(void)
>{
>    m_bDone = FALSE;                // This generates a compiler error.
>    CMyClass::m_bDone = FALSE;      // This generates a compiler error.
>}

If your function really needs to be static, and m_bDone really needs
to be non-static, then the only way the static function can touch an
m_bDone is through an object of your class or a  pointer or reference
to one. Non-static member variables exist only in objects of a class,
while static members exist on their own, even in the absence of any
objects of the class. Thus, without an object, a static member
function can only access other static members of the class.

--
Doug Harrison



Sun, 31 Dec 2000 03:00:00 GMT  
 static functions and non static variables

Quote:

> Hello All,

> I have a little problem that i hope some of you might be able to help me
> with.

> Basically i have a class which contains a function declared as static:

> // MyClass.h
> class CMyClass
> {
> public:
> // PUBLIC FUNCTIONS
>         static BOOL MyStaticFunction(void);
> // PUBLIC VARIABLES
>         BOOL m_bDone;
> };

> But the problem is i need to change the value of m_bDone in the function
> MyClass::MyStaticFunction(). Like this ...

> BOOL CMyClass::MyStaticFunction(void)
> {
>         m_bDone = FALSE;                // This generates a compiler error.
>         CMyClass::m_bDone = FALSE;      // This generates a compiler error.
> }

> If anyone knows a way around this problem, please get back to me either

> Thanks in advance,

> Conner.

The only way around it is to somehow get the address of the object or
member variable in the static function. If there is only one instance of
the class (and therefore only one m_bDone) you can simply store 'this'
into a static variable from any nonstatic class function.  If there are
multiple instances of the class you have to invent some structure where
the static function can find and identify which 'this' it should hit.
Either way, suppose the static function can find:
  CMyClass* pTheObject
Then it can use:
  pTheObject->m_bDone = FALSE;


Sun, 31 Dec 2000 03:00:00 GMT  
 static functions and non static variables

Quote:

>Hello All,

>I have a little problem that i hope some of you might be able to help me
>with.

>Basically i have a class which contains a function declared as static:

>// MyClass.h
>class CMyClass
>{
>public:
>// PUBLIC FUNCTIONS
> static BOOL MyStaticFunction(void);
>// PUBLIC VARIABLES
> BOOL m_bDone;
>};

>But the problem is i need to change the value of m_bDone in the function
>MyClass::MyStaticFunction(). Like this ...

>BOOL CMyClass::MyStaticFunction(void)
>{
> m_bDone = FALSE; // This generates a compiler error.
> CMyClass::m_bDone = FALSE; // This generates a compiler error.
>}

>If anyone knows a way around this problem, please get back to me either


I do it all the time.  I need to use static functions for, say, the message
loop of my window classes.  In that case, I have a static member variable of
type CMyClass* that I set to "this" just before I call the static function.
Be careful of multithreaded programs, though.

A better way would be to make the CMyClass* one of the parameters of the
static function.

I also use static functions to run threads with.  In that case, the function
has one void* parameter, which I use to pass the address of a structure,
that contains, amongst other things, a pointer to the CMyClass that called
it.

It's kind of a catch-22, these static functions.  But you'll figure it out.

BTW, why do you need the function to be static, if I may ask?

Steven Schulze
Concord,  CA



Sun, 31 Dec 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. static functions with non-static variables!

2. One Problem in Calling Static function from Non static function

3. Accessing non-static functions from a static function

4. Pointers to non-static vs static functions

5. Static array containing non static objects and functions

6. Static and non-Static member - Signature

7. Creating a static class from non static Framework classes

8. static versus non static returnvalues

9. Linking Error - Static vs. Non-Static

10. Linking Static DLL vs Non Static DLL

11. Getting pointer to non-static member function from C callback function

12. Using Non-Static Callback Functions as member Functions VC5.0

 

 
Powered by phpBB® Forum Software