Thread: matching one data type to another

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    4

    matching one data type to another

    I'm trying to come up with a better solution than the one shown below to match one data type to another. Frequently I see code such as below where a switch is done in order to match one type to another. The example shows two enums but they need not be.
    Code:
    #include <iostream>
    
    using namespace std;
    
    enum color
    {
    	BLUE = 0,
    	RED,
    	GREEN,
    	YELLOW
    };
    
    enum object
    {
    	APPLE = 0,
    	SKY,
    	FIRE,
    	SUN
    };
    
    object color_2_object(unsigned char color);
    color object_2_color(unsigned char object);
    
    int main(void)
    {
    	object object1;
    	color  color1;
    
    	cout << "Hello\n";
    	
    	object1 = color_2_object(YELLOW);
    	cout << "color YELLOW matches object: " << object1 << "\n";
    
    	color1 = object_2_color(APPLE);
    	cout << "object APPLE matches color: " << color1 << "\n";
    }
    
    object color_2_object(unsigned char color)
    {
    	object local_object = SKY;
    
    	switch(color)
    	{
    	case	BLUE:
    		local_object = SKY;
    		break;
    	case	RED:
    		local_object = FIRE;
    		break;
    	case	GREEN:
    		local_object = APPLE;
    		break;
    	case	YELLOW:
    		local_object = SUN;
    		break;
    	default:
    		break;
    	}
    	return	local_object;
    }
    
    color object_2_color(unsigned char object)
    {
    	color local_color = GREEN;
    
    	switch(object)
    	{
    	case	APPLE:
    		local_color = GREEN;
    		break;
    	case	SKY:
    		local_color = BLUE;
    		break;
    	case	FIRE:
    		local_color = RED;
    		break;
    	case	SUN:
    		local_color = YELLOW;
    		break;
    	default:
    		break;
    	}
    
    	return	local_color;
    
    }
    I've also come across situations where 'just by chance' the two enums match up and therefore a simple = is used.
    Code:
    enum color
    {
    	BLUE = 0,
    	RED,
    	GREEN,
    	YELLOW
    };
    
    enum object
    {
    	SKY = 0,
    	FIRE,
    	APPLE,
    	YELLOW
    };
    
    color1 = (color)object1;
    My concern is when or if one or both of the data types are changed. Multiple places (switches) must be changed and I'm trying to think of a good way to avoid this. Is the only solution to create these type of conversion or matching routines? I only tend to need to do the conversion once and therefore a procedure seems a little wasteful. However on the other hand there are many of these data types which I need to convert and I don't like adding many switches in the code.

    I've used some simple c++ here so please excuse!
    TIA

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you writing C or C++? This is the C board, and you're posting C++ code.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved to right board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    For integer based conversions I think a couple simple conversion functions would be best. Use the switch inside the conversion functions and just call the conversion functions wherever you need to.

    You could also use maps if you are converting non-integer data. Load the maps once and then your conversion functions can just do lookups.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or for enums you can use simple arrays.

    But there is no way around the fact that, every time you change the enums, you must remember to update the converter. There simply is no other way - the computer cannot magically know which value goes with which.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    4
    Thanks all.
    @CornedBee
    Or for enums you can use simple arrays.
    can you give an example please?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    color object_to_color[] = { BLUE, // SKY = 0
                                RED, // FIRE
                                GREEN, // APPLE
                                YELLOW // SUN
    };
    
    object color_to_object[] = { SKY, // BLUE = 0
                                 FIRE, // RED
                                 APPLE, // GREEN
                                 SUN // YELLOW
    };
    
    object color_2_object(color clr) { return color_to_object[clr]; }
    color object_2_color(object obj) { return object_to_color[obj]; }
    That requires you to keep the arrays accurate and the enums must be contiguous and starting from 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. getc and int data type
    By Zeiro in forum C Programming
    Replies: 3
    Last Post: 08-13-2008, 10:25 AM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM