Why can you access meters outside the scope?

Why can you access counters outside the scope while you cannot directly access members of structures directly? By area, I mean the area of ​​the declaration - Please correct me if I am wrong.

To give an example: You can do this:

enum colors {red, blue, white};
int color = red;

but you cannot do this:

struct colors {red, blue, white};
int color = red;

Thank!

+4
source share
2 answers

Well, well, as another comment said, this is because C ++ works.

In the old days, we modeled something like this with something like

class Colors {
  public:
    static int RED = 0;
    static int GREEN = 1;
    static int YELLOW = 2;
}

Then an enumeration was added, so you did not need to write Colors.RED. It is essentially syntactic sugar.

Update

- , . , : ++ , JavaSccript . , .

++, - , Algol 60, , . - . , , .

{}. , , .

#include <stdlib>  // copies a bunch of code into the file
int foo = 0;       // declared here, visible to end.

int fn(){
  int bar = 2 ;
  if(bar == 2){
    foo = bar;
    cout << bar << nl;  // gives '2'
    cout << foo << nl;  // still gives '2'
  }
  cout << foo << nl ;  // gives '0'
  cout << bar << nl ;  // compile time error 'bar' not defined
}

? foo foo, , if. bar if, ( " " ) , if

, ++.

+1

. enum , struct. ? . , , , , , .

enum class, , ; " ".

+1

Source: https://habr.com/ru/post/1676572/


All Articles