Thread: enum redeclaration

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    enum redeclaration

    I would have liked to do this:

    Code:
    typedef enum {
         Exp = 0x14110,
         Gain = 0x14150,
    } Type_Device1_RegisterAddress;
    
    typedef enum {
         Exp = 0x00110,
         Gain = 0x00150,
    } Type_Device2_RegisterAddress;
    but i receive compiler errors
    how would you suggest to dio something similar?

    i don't like much the following (cause it's something close to using defines):

    Code:
    typedef enum {
         Device1_Exp = 0x14110,
         Device1_Gain = 0x14150,
    } Type_Device1_RegisterAddress;
    
    typedef enum {
         Device2_Exp = 0x00110,
         Device2_Gain = 0x00150,
    } Type_Device2_RegisterAddress;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Would you like to use C++ instead? I do not think that C provides such a scoping mechanism, but I could be wrong since I am too used to what is allowed by C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    thank you but i am working on an embedded platform that supports only alpha C++ compiler so i prefer C

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In C, all the enum descriptors are in one namespace, so it's not possible to reuse names.

    I suppose it's going to depend on how you want to use these things whether explicit names like you have it will work or not. If you always know, when you're typing the variable name, whether you need device 1 or device 2, what's wrong with it?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I don't think this is possible in C either. But maybe a datastructure like this is better:

    Code:
    const struct {
    	int exp;
    	int gain;
    } devices[] = {
    	{ 0x14110, 0x14150 },
    	{ 0x00110, 0x00150 },
    };
    And then use "devices[0].exp".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 10:29 PM
  3. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  4. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM