2

It is said that a type name cannot be defined inside a class after being used. e.g:

typedef double Money;
class Account {
public:
    Money balance() { return bal; } // uses Money from the outer
private:
    typedef double Money; // error: cannot redefine Money
    Money bal;
    // ...
};

The program doesn't work on GCC but on MSVC 14 it works fine!?? * I got this example from "C++ primer 5th edition".

AdamMenz
  • 381
  • 1
  • 13
  • 1
    I think if you kept reading a couple of lines below in your book you'll ind the answer. "Although it is an error to redefine a type name, compilers are not required to diagnose this error. Some compilers will quietly accept such code, even though the program is in error.". – Raindrop7 May 17 '19 at 00:29
  • Conformance view in godbolt is quite useful for this type of question: https://godbolt.org/z/tzYN-p – xaxxon May 17 '19 at 00:31

0 Answers0