1

After a lot of debugging I discovered a typing error, but am surprised the compiler let me away with naming a variable the same as a class name. Should that be allowed? This is a cut-down re-enactment of the crime. I used Phone as a pointer to a different class.

    class PhoneData 
    {
    public:
    PhoneData(){};
   ~PhoneData(){};
   int speed;
   };

   class Phone
   {
   public:
      Phone(){};
      ~Phone(){};
   int type;
   };

    int main()
   {
   Phone phone;
   PhoneData *Phone = new PhoneData();
   Phone->speed=1;

   return 0;
   }
Michael
  • 11
  • 1
  • 1
    You can, but it's invariably a bad idea as it makes code much harder for humans to read. Note that since C++ is case-sensitive a common convention is to use `PascalCase` for types (classes, structs, etc) and `camelCase` for locals/variables/parameters and instance fields - why not do that instead? (The STL designers prefer `snake_case` for STL types though). – Dai Feb 02 '21 at 02:34
  • ... what's the question? If the question is "is it allowed" then yes, you already figured that out. If it's "which section in the standard says that it's allowed" then it's probably okay (but the standard is terribly hard to read anyway). If it's "should that be allowed", then we can't do anything, discuss it on the C++ mailing list. – user202729 Feb 02 '21 at 02:45
  • Okay, thanks - will be a bit more watchful on the variable names. – Michael Feb 02 '21 at 03:16

0 Answers0