0

If it's an ordinary local object, its scope begins when it's declared, and things before its declaration can't access it. However, if it's a member variable, then anything in this class, even written before the declaration of that member, has access to it.

What happens under the hood? Why is it designed this way?

I tried to google about it but only got the rule itself instead of the mechanism or the motive.

  • 1
    It's basically the same basic reason why water's wet, the sky's blue, etc... It was "designed this way" because it was designed this way. That's how it works. – Sam Varshavchik Oct 28 '22 at 02:00
  • I'm certainly glad it's designed that way. To me it's quite intuitive that class member scopes encompass the class. – doug Oct 28 '22 at 02:03
  • It has a lot to do with language processing design as well. Originally, C was an LL(K) language, so everything had to be properly declared _before_ it was used. Modern C++ requires a much more complicated parser, with multiple passes, so class constructs with inline code declarations using not-yet declared members are a lot more reasonable. – Dúthomhas Oct 28 '22 at 02:08
  • It's the way the language is designed. It avoids making a lot of things unnecessarily difficult for the programmer (e.g. allowing the programmer reasonable latitude to decide where to implement a member function inline in the class definition, rather than only allowing it in particular places). Implementing that is a property of parsing - for example, the code (e.g. in a compiler) that reads a class definition is required to scan the whole class definition before performing syntactic and lexical analysis (or behave as if it does). – Peter Oct 28 '22 at 02:25
  • 1
    *"However, if it's a member variable, then anything in this class, even written before the declaration of that member, has access to it."* -- This is demonstrably false: the definition `class Foo { decltype(member) thing; int member; };` does not compile because `member` is used before it is defined. Perhaps you should be more specific than "anything"? If you need inspiration for a list, see [Can't use nested class before its declaration inside the enclosing class](https://stackoverflow.com/questions/72673903/). – JaMiT Oct 28 '22 at 05:02
  • (I will grant that the places in a class definition where a member variable cannot be used before it is declared are obscure. It is possible that you did a reasonable amount of experimentation before concluding "anything". Possible. My nominal point is that you did not demonstrate what you tried. A bit of code to illustrate a question is often a good idea.) – JaMiT Oct 28 '22 at 05:30

1 Answers1

0

There's nothing to answer here. It is just that way.

Under the hood happens that the compiler tracks the scope as specified by the language.

Why that language is that way: I'd say it is intuitive, but you seem to disagree, so the only argument I could come up with has been devalued by the very question you're asking. .

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94