gladys0313
gladys0313

Reputation: 2699

C++: Can I assign the value of non-static member variable to static member variable?

I have a class A in which I have a static member function passName

int A::passName()
{
  .... // skip some code
  std::string name = ...; // result from codes above
  assign(); // this is a static member function in class A
  pointerA->passMethodName(name); // pointerA is a class-A static member variable, but of type 
                                 // class-B, passMethodName is a class-B non-static member function.
}

The assign function is:

void A::assign(){
  pointerA = tempPointerA;
}

Explanation: tempPointerA is a value that is generated during the running process. It is a non-static private class-A member which will be initialized everytime a new object of class A is constructed. But I know in static function I can only use static member directly, so I need to make sure that pointerA is static member. So is assign() function feasible (Or I would rather say, is the whole working principle shown here feasible)?

Thanks for your idea!

Upvotes: 2

Views: 2152

Answers (3)

SHR
SHR

Reputation: 8313

There is one rule: static functions can't access non-static members and function of the same class without an object. there is no opposite rule. it is because you don't have a this pointer.

You still can declare an object of the same class in the static function and use it's all members, or use any static member.

Therefore from non-static function you can access static functions and members.

If pointerA is static you can access it all. not only to it's static members and functions.

Upvotes: 0

AndyG
AndyG

Reputation: 41110

No. A static member function can only operate on static variables or call other static functions. (or namespace-scope functions, which are more or less the same as static functions).

§9.4.1 [class.static.mfct]

A static member function does not have a this pointer.

So there is no way to access a non-static member variable within a static function.

If you really need assign to remain static, then what you should do is to refactor yourassign()function to accept a variable of typetempPointerA`, and then pass your desired variable in.

int A::passName(B* _in)
{
  std::string name = ...; // result from code above
  assign(_in); // this is a static member function in class A
  _in->passMethodName(name);
}

Otherwise I recommend that you not make it static at all.

Upvotes: 2

Sorin
Sorin

Reputation: 11968

Assigning the value will work, but you need to do it from a non-static method (to have access to tempPointerA) or pass the pointer as paramter to the static method. You can access static members from non-static functions (but not the other way around).

What you should pay attention is ownership and destruction. Since you assign the value to a static member the instance can't own the value anymore. Otherwise when the instance is destroyed the static member points to garbage data and you get errors. Also since the static member is never destroyed, your value may leak resources (think DB connection that's never closed).

Also if you are in multi-threaded environment consider if it's possible that multiple threads will attempt to set the value of the static member. You may have the add synchronization. You can also run into race conditions if multiple threads try to initialize the value at the same time.

Upvotes: 0

Related Questions