Thread: Simple yes/no question regarding classes

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    22

    Simple yes/no question regarding classes

    Can you have a class object with the same name as the class?

    Example:

    Code:
    class Person {
    
    public:
    
    	struct person {
    	char name[20];
    	int id;
    	};
    };
    I ask because the compiler doesn't seem to understand person.id, etc.

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    no, thats illegal as your compiler tells you

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    Okay. I figured it must be. Thanks. I was doing an assignment, which was to convert a program to a class-based program. I decided to ditch the struct alltogether as it is sort of redundant now.
    Last edited by Ricochet; 12-11-2003 at 02:22 AM.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Actually, that class is legal as you have it, since 'Person' is different than 'person' (C++ is case-sensitive). It is illegal for them to be the exact same (with the same case). Of course, I don't think its a good idea anyway to be naming two different things with the same name differing only by case, so its probably a good idea that you changed it.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Simple yes/no question regarding classes

    Originally posted by Ricochet
    Can you have a class object with the same name as the class?

    Example:

    Code:
    class Person {
    
    public:
    
    	struct person {
    	char name[20];
    	int id;
    	};
    };
    I ask because the compiler doesn't seem to understand person.id, etc.
    That's because you must access it as
    Person.person.id
    It'd really be best to come up with better identifiers, though.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    Oh okay. Well that's interesting to know at least. Though I think I'll try not to do it. I just got rid of the struct alltogether. It made things easier inside and outside the class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Classes Question
    By kbro3 in forum C++ Programming
    Replies: 9
    Last Post: 08-14-2008, 07:43 AM
  2. Simple C# structure question
    By sketch in forum C# Programming
    Replies: 4
    Last Post: 09-14-2007, 04:29 PM
  3. Question about Classes
    By WildFire in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2004, 07:57 PM
  4. Flowchart question: Classes
    By darnok in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2004, 06:25 PM
  5. Simple syntax question on classes
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2002, 08:30 PM