0

How does the computer make sure that a member of a structure cannot be accessed without using an object of that structure? Maybe this badly worded, so here is an example: We have this struct:

struct products 
{
int a;
int b;
} apples;

This can only be accessed through an object of the structure, which in this case is apples:

int main ()
{
apples.a = 20; //the member a can be accessed through apples and a would be another variable if accessed through another object, why is that?
return 0;
}

Same goes for classes... So how does the computer(not sure what is handling this) make sure that products's members can only be accessed through an object of that struct or class(type)?

Thanks!

  • 5
    Computers deal with zeroes and ones, they don't care about how you access stuff. It's the compiler's job. If you do it the "wrong" way (against the rules of the _language_), the compiler warns you or throws an error at you – ForceBru May 13 '17 at 20:28
  • 2
    Well, of course apples.a means the a of apples, and pears.a means something else so it won't access the a of apples... how can that not be clear? ... If you mean at runtime, the computer does not make sure. It's entirely possible, and a valid use case, to access a without having "apples.a" in the code. – deviantfan May 13 '17 at 20:29
  • 1
    All that fancy `struct products {} apples;` thing is just "meh" for the computer as it doesn't understand it. It has only been created for _you_, a human, to use. All these concepts of objects, `struct`s, classes, integers, `double`s and stuff - all of this is _just for you to understand and work with_, the computer doesn't care. – ForceBru May 13 '17 at 20:35
  • @deviantfan that said, the vmm of the OS should prevent you from runtime egregious violations at runtime, let trying to access addresses that mapped to your process. – Alex May 13 '17 at 20:58
  • Possibly related: http://stackoverflow.com/q/43946981/990142 – rwols May 13 '17 at 21:46

4 Answers4

1

The idea is that you need an instance of the struct/class in order to access the members of the struct/class

When you write

struct products
{
  int a;
  int b;
} apples;

you declare an instance of the struct products in memory (depends on where the declaration is)

 +---+---+
 | a | b |
 +---+---+

writing just

 struct products
 {
   int a;
   int b;
 };

does not create an instance of the struct, instead you have told the compiler that there is a struct called products that has two members a and b the struct is in effect a type.

AndersK
  • 35,813
  • 6
  • 60
  • 86
0

The class members are created when an object is instantiated. The exception to this fact is when a member is defined statically (with the prefix "static" in front of the declaration). In this case the static member is available also when no object is not instantiated. An example:

class A {
static int x;
int y;
};

In this case you can access x without having an object, e.g.

A::x = 7;

Notice the different syntax when accessing a static member...

R. Daisy
  • 11
  • 1
0

When you do

int main() {
    apples obj1;
    apples obj2;
}

obj1 and obj2 are distinct objects: they exist in different parts of the memory, i.e. &obj1 != &obj2. Then, assuming that int is 4-byte type, obj1.a would be the int at &obj1 + 0 and obj1.b the int at &obj1 + 4 bytes and obj2.b the int at &obj2 + 4 bytes.

The compiler is responsible for converting names ("obj1" and "obj2") into memory locations. (That is, as far as the stack is concerned.)

p.s. That's about objects. Not sure if you're asking about classes instead. In this case the answer is that when you write a, this is an unqualified name. The compiler, depending on the context (i.e. if it's in the body a member function of class Apple or if you are writing my_apple_object.a) will know that you're actually referring to Apple::a and not to Blueberry::a. Likewise if the same a token appeared in the body of a member function of Blueberry, the compiler would know that you're referring to Blueberry::a. And if the context is ambiguous the compiler will complain and/or issue an error.

n.caillou
  • 1,263
  • 11
  • 15
0

struct and class both provide access restrictions in the form of private:, public:, and protected:. Anything you declare behind one of the qualifiers becomes just that.

  • public means you can access it the way you specified - no protection;
  • protected means only the class and classes derived from it can access it;
  • private means only the class itself can access it.

The only difference between struct and class is that struct starts with the default set to public:, and class starts with private: active. You can change the setting anywhere in the class definition, at the beginning or between declarations, for example:

struct products
{
  private:
    int a;
    int b;
} apples;

makes a and b inaccessible from outside the class.

Aganju
  • 6,295
  • 1
  • 12
  • 23