C ++: class member names in the lookup


#include <iostream>
#include <string>
using namespace std;

typedef double Money;
string bal;
class Acount
{
public:
    Money balance()
    {
        return bal;//bal的类型?
    }

private:
    Money bal;
};

int main()
{
    return 0;
}

Question: As an example, the type of bal what?
answer:

  1. When you define a class, the compiler to compile a statement of its members until all visible after the class compiled function body. Because the body of the function bal, so look at the statement;
Money balance()
    {
        return bal;//bal的类型?
    }
  1. Only statements of balance and bal in class, not declared for the Money, we need to look for enclosing scope Money.
Money balance()
Money bal;
  1. Money found in the outer scope actually double.
typedef double Money;

  Therefore, in the class declaration similar to:

double balance()
double bal;
  1. From the type declaration type bal it is double, rather than the outermost string.

to sum up:

  1. When you define a class, the compiler to compile a statement of its members until all visible after the class compiled function body.
  2. General name lookup procedure: first in the block where the name and the prior use of the name lookup statement code; if not found, the scope continues to look to the outer layer; if the outermost scope did not find a matching name statement, the program error.
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/104861173