Why can't redefine type names in class in C++?

前端 未结 3 1260
星月不相逢
星月不相逢 2020-12-03 23:28

According to the book C++ Primer section, 7.4.1 Type Names Are Special:

Ordinarily, an inner scope can redefine a name from an outer scope even if tha

3条回答
  •  爱一瞬间的悲伤
    2020-12-04 00:17

    When the compiler reads the line

        Money balance() { return bal; }
    

    in the class definition, it already uses the definition of Money outside the class. That makes the line

        typedef double Money;
    

    inside the class a problem. However, you may use redefine Money inside the class before it is used in the class. The following is OK.

    typedef double Money;
    
    class Account {
        public:
            typedef double Money;
            Money balance() { return bal; }
        private:
            Money bal;
    };
    

    The key point in the quote is:

    hence the class may not subsequently redefine that name.

提交回复
热议问题