Consider the following code:
#include <iostream>
using namespace std;
class A
{
private:
int x;
public:
A(int _x) { x = _x; }
int get() { return x; }
};
class B
{
static A a;
public:
static int get()
{ return a.get(); }
};
int main(void)
{
B b;
cout << b.get();
return 0;
}
My doubt here pertains to this line of code:
public:
static int get()
{ return a.get(); }
As per this link: Why can I only access static members from a static function?, a static
function can only access a static
data member. However, in this function, we are returning a.get()
. The function get()
in class A
, returns x
, which is a non-static
variable. So, does this not contradict the fact that static function can only access static data members?
Please guide.