Thread: Enum as array

  1. #31
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by antred View Post
    That's not my point. Let me show you what I was getting at.
    I was apparently in the middle of my point when you stopped reading.

    Code:
    State* states = 0;
    ...
    if ((states = new (std::nothrow) State[size]) != 0)
       ...
    I don't really find fault with code like this, although, like I said, new generally throws when it fails so you might not want to do this anyway. There is no way you could dereference states and cause problems. It's an idiosyncrasy that is about as indefensible as others.

  2. #32
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    Quote Originally Posted by Dontgiveup View Post
    Not sure that is a good idea. I think, and someone correct me if I am wrong, in C++, unless you assign values, everything remains undefined.
    bool

    int

    float

    double

    Seems like all have been initialized by default.

  3. #33
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by R41D3N View Post
    bool

    int

    float

    double

    Seems like all have been initialized by default.
    I can do that too.
    C++ code - 11 lines - codepad

    Context matters. In this case the only reason that bool arrays may be initialized is because you wrote a new statement in the initializer list. Ordinarily, plain old data is not constructed at all, but the compiler can generate constructors for them, and they can be called.

    I don't recommend calling new in the initializer list anyway. Because you have to handle the exception that might be thrown. And I seem to recall that code not working for microsoft compilers.

  4. #34
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by whiteflags View Post
    That is actually initializing (afaik), the values are 0 within the error margins of double.

  5. #35
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's not. If it were initialized, it would say zero. It so happens that zero is also a common value in memory, so if you take a random selection of bytes and treat them as doubles, you will likely get (unnormalized) values close to zero.

  6. #36
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I can do that too.
    I can too! Mine is better!

    Soma

    Code:
    #include <iostream>
    
    using namespace std;
    
    void T1()
    {
        float a;
        cout << a << '\n';
    }
    
    void T2()
    {
        float a(4.0f);
        cout << a << '\n';
    }
    
    void T3()
    {
        float a;
        cout << a << '\n';
    }
    
    void T4()
    {
        int a;
        cout << a << '\n';
    }
    
    void T5()
    {
        int a(16);
        cout << a << '\n';
    }
    
    void T6()
    {
        int a;
        cout << a << '\n';
    }
    
    void T7()
    {
        int * a(new int[10]);
        for(unsigned int c(0); c < 10; ++c)
        {
            cout << a[c] << '\n';
        }
        delete[] a;
    }
    
    void T8()
    {
        int * a(new int[10]);
        for(unsigned int c(0); c < 10; ++c)
        {
            a[c] = c;
            cout << a[c] << '\n';
        }
        delete[] a;
    }
    
    void T9()
    {
        int * a(new int[10]);
        for(unsigned int c(0); c < 10; ++c)
        {
            cout << a[c] << '\n';
        }
        delete[] a;
    }
    
    int main()
    {
        T1();
        T2();
        T3();
        T4();
        T5();
        T6();
        T7();
        T8();
        T9();
        return(0);
    }
    Code:
    6.00414e-039
    4
    4
    1082130432
    16
    16
    4063968
    4063968
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

  7. #37
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    Sorry, turns out it was the compiler in that site. Tried the same code with MinGW and it returns garbage :P.

    @Dontgiveup: Your best bet then is fill()

  8. #38
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by whiteflags View Post
    I was apparently in the middle of my point when you stopped reading.
    Yes, I may have misread your post. Sorry about that. But still, the example you gave doesn't preclude putting declaration and initialization in one statement:

    Code:
    if ( States* states = new (std::nothrow) State[size] )
    {
    	// ...
    }

  9. #39
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I didn't mean to preclude anything, but wouldn't that make the scope of states really weird, as in the if statement block?

  10. #40
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Well yeah, it would only be accessible in the if and else blocks. If you need access beyond that scope then this method isn't applicable.

  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    @OP:
    You can assign enums to other variables because they are treated as ints due to their C heritage.
    Yes, that means you can assigns Cars to Fruits (assuming enums here) too!
    That said, this is all bad practise, so do not do it.
    You may also want to read about strong enums and initializer lists (both C++11 features).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #42
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by Elysia View Post
    @OP:
    You can assign enums to other variables because they are treated as ints due to their C heritage.
    Yes, that means you can assigns Cars to Fruits (assuming enums here) too!
    That said, this is all bad practise, so do not do it.
    You may also want to read about strong enums and initializer lists (both C++11 features).
    Thanks, so I guess the answer to my question is no. That it is not possible to initialise a dynamic array in an initialisation list. Instead of, as am doing now:

    Code:
    class Test {
    bool * test;
    
    public:
    void init() {
    test = new bool [10];
    for(int i = 0; i < 10; ++i) test[i] = false;
    Test () : test(0) {init();}
    };
    something like:

    Code:
    class Test {
    bool * test;
    
    public:
    Test () : test(new bool [10]{false})//initalise it with default false values
    };

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It most definitely is possible. I haven't tried this myself, but something like this should work:
    Code:
    class Test
    {
    	std::vector<myenum> m_Vec;
    	Test(): m_Vec({ e1, e2, e3, ... }) {}
    };
    Requires a modern compiler with C++11 initializer lists support.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #44
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by Elysia View Post
    It most definitely is possible. I haven't tried this myself, but something like this should work:
    Code:
    class Test
    {
    	std::vector<myenum> m_Vec;
    	Test(): m_Vec({ e1, e2, e3, ... }) {}
    };
    Requires a modern compiler with C++11 initializer lists support.
    Unfortunately, we can't use predefine data structure. This is part of a programming assignment where we are supposed to make our own hashtable (add remove, sorting etc.) using our own functions, STL data structures not permitted. I have done everything well and so far all is working well. I was just on the lookout for inefficiencies in my program, and what, where and how I maybe able to improve.

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't actually need to use a std::vector to use that (it was just an example), but the bigger problem is that you are not terribly likely to be using a compiler and supports the latest version of the C++ standard.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 02:32 AM
  2. enum and 2D array declaration
    By bigbig in forum C Programming
    Replies: 2
    Last Post: 03-30-2010, 11:59 AM
  3. enum constants as array indices
    By hzmonte in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 08:23 PM
  4. enum Help
    By Bitphire in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2004, 11:03 AM
  5. What enum got to do with it?
    By correlcj in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 08:12 PM