Thread: Casting enumerated type to integer query

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    216

    Exclamation Casting enumerated type to integer query

    Hi,

    I've come across something in my learning which has puzzled me. I won't go too far into the details, take a look at this:

    Code:
    enum weekday
    {
        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY
    };
    
    weekday day = static_cast<weekday>(0);
    This is to get around a compiler warning of invalid conversion type. Why would you want to do this though, and what does it even achieve?

    Pretty sure it's crazy to cast a plain integer to an entire enumerated type. Can anyone shed any light on this?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,652
    Pretty sure it's crazy to cast a plain integer to an entire enumerated type.
    What do you mean by "entire enumerated type"?
    Is it crazy to cast a "plain integer" to an "entire floating point type"?

    Do you understand what an enumerated type is?
    Enumeration declaration - cppreference.com
    Last edited by john.c; 06-09-2024 at 09:46 AM.
    And that's the world in a nutshell, an appropriate receptacle.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    216
    Hi there,

    I had a little read John and it makes more sense now (I think). If using a typical enumerated type list starting from zero and going up by one each time, the variable 'day' would be set to the first item on the list. In this case it would then correspond to SUNDAY. So an enumerated type just uses a list of integers with some strings as aliases for those integers.

    Still not why you couldn't just assign it though, and why you need to use a cast instead

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,652
    Still not why you couldn't just assign it though, and why you need to use a cast instead
    You can assign it 0 in C without a cast, but the C++ type system is more restrictive so that things like function overloading (which doesn't exist in C) work properly. Also, if you can easily assign an integer to a Weekday type you can easily assign an incorrect value, i.e., one that doesn't have a constant name defined. It's best to assign one of the constants directly.
    Code:
    #include <iostream>
    
    enum Weekday { Sunday, Monday, Tuesday };
    
    void print(Weekday d) {
        using std::cout;
        cout << "Weekday: ";
        switch (d) {
        case Sunday:  cout << "Sunday";  break;
        case Monday:  cout << "Monday";  break;
        case Tuesday: cout << "Tuesday"; break;
        }
        cout << '\n';
    }
    
    // Same function name, different argument type
    void print(int d) {
        std::cout << "int: " << d << '\n';
    }
    
    int main() {
        Weekday day = Sunday;
        print(day);
        print(0);
    }
    Or even something like the following where operator<< has many overloads.
    Code:
    #include <iostream>
    
    enum Weekday { Sunday, Monday, Tuesday };
    
    std::ostream& operator<<(std::ostream& os, Weekday d) {
        switch (d) {
        case Sunday:  os << "Sunday";  break;
        case Monday:  os << "Monday";  break;
        case Tuesday: os << "Tuesday"; break;
        default: os.setstate(std::ios_base::failbit);
        }
        return os;
    }
    
    int main() {
        Weekday day = Monday;
        std::cout << day << '\n';
    }
    And that's the world in a nutshell, an appropriate receptacle.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    216

    Thumbs up Thanks!

    That's great thanks very much John. Kinda spooky you provided an overloaded operator example too, I have literally just in the last week covered that in my online course lol!! Otherwise I wouldn't have understood it. Thanks very much again John, I'm gonna have a play with that in the IDE. Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring enumerated data type
    By marystew in forum C Programming
    Replies: 1
    Last Post: 11-08-2020, 11:12 PM
  2. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  3. 2-dimesional Array w/ enumerated type
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2007, 01:22 PM
  4. difference between type conversion and type casting
    By Bargi in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 03:17 AM
  5. enumerated type and lvalue missing
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2006, 07:06 AM

Tags for this Thread