Workaround for accessing non-static members inside static member functions

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Workaround for accessing non-static members inside static member functions

What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B.

Re: Workaround for accessing non-static members inside static member functions

Can you post a simple example of what you are trying to achieve.

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

Re: Workaround for accessing non-static members inside static member functions

Originally Posted by hemant.bhargava7

What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B.

Any pointers?

To access the non-static member variables of some class wrom anywhere you need to have a pointer or reference to some instance of the class which member you are going to access.

Re: Workaround for accessing non-static members inside static member functions

Originally Posted by hemant.bhargava7

What are the workarounds for accessing the non-static member variables of some class(Say A)

There is no such thing as a "workaround" here. The rules of C++ are specific -- to access non-static members of a class from outside the class itself:

a) The members must have an access specifier that makes them accessible (public for unrelated classes, public / protected for derived classes).
b) You must have an instance of the class A (as VictorN pointed out) to access non-static members.

Also on a design level, why are you allowing the client (class B) to access member variables of class A? Accessing member variables directly breaks encapsulation, i.e. if that member variable changes (for example, the name changes), the client code has to change. Better to use get() / set() functions and have the client call those functions instead of accessing member variables directly.

Class A is derived with public properties of class B.

The only C++ terms in that sentence are "class" "derived" and "public". There is no such thing as "properties" in C++. So I don't understand what you're trying to say here.

Re: Workaround for accessing non-static members inside static member functions

Originally Posted by Paul McKenzie

There is no such thing as a "workaround" here. The rules of C++ are specific -- to access non-static members of a class from outside the class itself:

a) The members must have an access specifier that makes them accessible (public for unrelated classes, public / protected for derived classes).
b) You must have an instance of the class A (as VictorN pointed out) to access non-static members.

Also on a design level, why are you allowing the client (class B) to access member variables of class A? Accessing member variables directly breaks encapsulation, i.e. if that member variable changes (for example, the name changes), the client code has to change. Better to use get() / set() functions and have the client call those functions instead of accessing member variables directly.
The only C++ terms in that sentence are "class" "derived" and "public". There is no such thing as "properties" in C++. So I don't understand what you're trying to say here.

Are you saying that B is publicly derived from A?

Code:

class A
{
};
class B : public A
{
};

Is it this?

Regards,

Paul McKenzie

Paul, Again thanks for quick reply. Yes. It is same as you expected.
class A {
public:
var_a;
}
class B : public A {
static func_a();
}
B::func_a() { // Need to use a; }

Re: Workaround for accessing non-static members inside static member functions

Conceptually it doesn't make any sense. Since it isn't static, a distinct var_a exists for every instance of A. If there are multiple instances, there are multiple var_a variables. If no instances exist, neither does var_a. What value are you expecting it to hold, or how are you expecting it to hold a value at all?

Re: Workaround for accessing non-static members inside static member functions

In order for the static function func_a defined as part of class B to access the variable var_a defined in class A, the static function func_a needs either a reference or a pointer to the required instance of class A in order to obtain the required value. As var_a defined in class A is not static, its value is only defined as part of a class instance (ie when a class variable is defined).

All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!