Thread: enum declaration~

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question enum declaration~

    Howdy~~

    I get used to declaring everthing in 2 lines such as follows:

    #include <iostream>

    void main()
    {
    enum pet;
    pet={dog,cat,bird,crocodile};
    // I heard someone was fond of crocodile, aha~
    pet myPet=dog;
    cout << myPet << endl;
    }

    but the output report an error. it works fine when I rewrite the code like this:

    #include <iostream>

    void main()
    {
    enum pet{dog,cat,bird,crocodile};
    pet myPet=dog;
    cout << myPet << endl;
    }

    why couldnt we delare it in 2 different lines as before ???
    Never end on learning~

  2. #2
    well it looks like it's written correctly, except myPet isn't declared. What exactly does the compiler tell you when you try to compile that?

    I don't think you should declare an enum inside of a function anyway.

    PS: I've never seen anyone do that first method anyway.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by frenchfry164
    I don't think you should declare an enum inside of a function anyway.
    then what should it be ?
    Never end on learning~

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I don't think you should declare an enum inside of a function anyway.
    That is just flat wrong. The whole point of having enum's is to 'clean-up' the interface of the code. So when faced with the question - USE THEM! ( inside or outside of functions... )
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    enumerations can only be declared using the second method. Essentially, you're enum is a type, not a variable. That's why you can't assign values to it, you have to define it's contents before it's used.

    And by the way, delcaring everything in two lines is a bad thing. Initialise your variables with the values you want if you can.

    Why?

    Coz if you do this:

    int i = 0;

    then i's constructor receives 0 as a parameter and it's initialised with that value.

    if you do this:

    int i;
    i = 0;

    i is constructed, and after that happens the assignment operator is called for i, so instead of just one function, there's two being called.

    So, if you can, use the first method, not the second.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question

    Originally posted by Uraldor

    Coz if you do this:

    int i = 0;

    then i's constructor receives 0 as a parameter and it's initialised with that value.

    if you do this:

    int i;
    i = 0;

    i is constructed, and after that happens the assignment operator is called for i, so instead of just one function, there's two being called.

    So, if you can, use the first method, not the second.

    U.
    but......it seems nothing serious happens if I declare a variable in 2 lines. does it cause any unexpected errors ?

    PS: please forgive my poor comprehension Caz' I am fresh to C++.
    Never end on learning~

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    it seems nothing serious happens if I declare a variable in 2 lines

    you dont declare variables in two lines. One line you declare it, another line you store things in it.

    does it cause any unexpected errors ?

    absolutely not, why would it? I'm just trying to tell you that declaring in one line, and storing in another is twice as intensive processor-wise as declaring and storing on the same line.

    If you do it in the one statement, then the value gets stored during construction.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  8. #8
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx Uraldor~

    And I still wanna know whether those which are not simply data types should only be delclared in 1 line as I tested enum before ?
    Never end on learning~

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