views:

936

answers:

7

I have two questions.

In C++, a static member function has direct access to a public non-static data member defined in the same class? False

In C++, a non-static member function has direct access to a private static data member defined in the same class? True

My note say false for the first question and true for the second one. I just cannot find out why? Can you explain why this is? Thank you.

P.S. I'm studying for my final and I cannot seem to figure out why.

+5  A: 

Here's a hint: Recall that a "non-static data member" refers to a data member of a particular instance of your class. A static member function does not run in the context of any particular instance.

Greg Hewgill
+3  A: 

Static member functions cannot access instance variables (non-static data), because instance variables need an instance of the class to operate on.

Remember that static data members or functions are defined and allocated once (not per instance), and hence can be accessed by non-static functions just as you would access global variables, etc.

(Internally, static functions don't get passed a this pointer like regular member functions. I.e. they use a different calling convention. Due to this they can't reference this->foo which is what really happens when you reference a member foo in a member function.)

Alex
A: 

In addition to Greg Hewgill's answer, you can think of the static functions as having a more narrow scope - i.e. member functions have access to everything static functions do AND all the instance variables. Static functions though can only access static members, which is logical enough.

class MyClass {

  static int m_iStatic;      
  int m_iInstance;

  static void StaticFunc() {
    m_iStatic = 8; //OK
    m_iInstance = 8; //not OK
  }

  void InstanceFunc() {
    m_iStatic = 8; //OK
    m_iInstance = 8; //OK
  }
}
Igor Zevaka
A: 

In C++, a static member function has direct access to a public non-static data member defined in the same class? False

Static member functions cannot access non-static data of the class due to the fact that static member function is not bound to the instance. For that matter static function can be accessed without any object. Any object specific data cannot be accessed in static member function.

In C++, a non-static member function has direct access to a private static data member defined in the same class? True

Again, since static data member does not belong to any particular object it can be accessed by all instances of the class.

aJ
A: 

to make things easier, lets strip the public/private away.

To access data member (attribute) of an object, you will need to know who/where is the 'object'. A static member function are shared across among objectS so it will need additional information when you ask him to grab the data member.

image object as family, child is the data member, boardSchoolBus is the static function. every children can board the school bus, but if you asked school bus to fetch a child, it would need to know which family to go right?

YeenFei
+11  A: 

Everyone's in agreement, but should be very careful about their wording, because actually static member functions do have access to public non-static data members. For that matter, they have access to private non-static data members too. They just need an object to operate on, to access its members. This could be a parameter, or a global, or created in the static member function, or acquired via one of those things.

The following code is fine:

class foo {
public:
    int a;
    // static member function "get_a" ...
    static int get_a(foo *f) {
        // ... accesses public non-static data member "a"
        return f->a;
    }
};

So we ask ourselves, what's the difference between "access" and "direct access"?

I guess what's meant by "direct access" here must be "using only the name of the data member, without specifying an object". Everyone always needs to have an object in order to access non-static members - that's what non-static means. Non-static member functions just don't have to mention which object if they don't want to, because this is implicit. Hence their access to non-static data members can be direct.

The reason non-static member functions have direct access to private static data members is firstly that the code is in a member of the class, hence it can access private data members. Second, you never need an object in order to access static data members (you can specify one if you want, but all that's used is the static type of the expression, not the actual object), hence the access is direct.

Steve Jessop
A: 

Many object oriented pundits/pandits would tend to silently say, you've got it wrong. Wrong, not because the answer is wrong but the thinking process needs to be resequenced.

Let's say you are a submarine designer. You have designed the Nehru class submarine. You have the blue prints but not the submarines. On blue print of Nehru class, you have the designer's name - Sonia Gandhi. So now people could STATICally refer to Nehru->designer which yields the value "Sonia Gandhi".

Now, every submarine has a captain. Since no submarine has been built yet, you cannot refer to any captain and therefore the reference Nehru->captain is not logical.

Then you build 10 Nehru class submarines, each assigned a captain. Some of the submarines are the Mumbai, the Delhi, the Rafael Jacob, the Rishi Kapoor.

You still cannot refer to Nehru->captain to get any of the ten captains' names. You could say Delhi->captain, Mumbai->captain or Rishi Kapoor->captain and get the respective captain's name, but there would not be any such valid reference as Nehru->captain because the Nehru reference is a class design and not a ship.

However, you could refer to Delhi->designer or Mumbai->designer or Rafael Jacob->designer which will yield "Sonia Gandhi" just as Nehru->designer would.

There, got it?

Blessed Geek