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.
Any pointers?
Re: Workaround for accessing non-static members inside static member functions
Can you post a simple example of what you are trying to achieve.
Re: Workaround for accessing non-static members inside static member functions
Quote:
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
Quote:
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.
Quote:
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.
Are you saying that B is publicly derived from A?
Code:
class A
{
};
class B : public A
{
};
Is it this?
Regards,
Paul McKenzie
Re: Workaround for accessing non-static members inside static member functions
Quote:
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
Quote:
Originally Posted by
hemant.bhargava7
B::func_a() { // Need to use a; }
This is rule b) I mentioned above. There is no instance of an A or B object in that code.
Regards,
Paul McKenzie
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).
The code below is an example of using int_a from func_a
Code:
class A {
public:
int var_a;
A() : var_a(0){}
};
class B : public A {
public:
static int func_a(A* ia);
};
int B::func_a(A *ia)
{
// Need to use a;
return (ia->var_a);
};
int main()
{
A ia;
B::func_a(&ia);
}