Thread: Question about efficiency... structures and stuff

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Question about efficiency... structures and stuff

    Alright, I have the following code:

    Code:
    class winStrct_T {
    public:
    	struct BLACK_t {
    		int r;
    		int g;
    		int b;
    	} BLACK;
    	struct WHITE_t {
    		int r;
    		int g;
    		int b;
    	} WHITE;
    	struct GREEN_t {
    		int r;
    		int g;
    		int b;
    	} GREEN;
    	struct BLUE_t {
    		int r;
    		int g;
    		int b;
    	} BLUE;
    	void set_colours(void);
    protected:
    	int tmpVar;		
    } winStrct;
    and it works, but I'm wondering if there is a more efficient way to do this? I don't know too much about unions, or structure pointers, however I feel that there's probably a way to improve this code...

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You could reduce the amount of space it takes by using a smaller data type to store each colour component, or pack all three into one integer. You'd then probably want to create functions to insert and extact values using bit masks.

    Also you could save typing by representing it as an array of COLOR' s:

    Code:
    class winStrct_T {
    public:
    	struct COLOR{
    		unsigned long XRGB;
    		} BLACK,WHITE,GREEN,BLUE;
    	void set_colours(void);
    protected:
    	int tmpVar;		
    } winStrct;
    Depending on what you are using the class for, you could also use something like a look-up table storing the different possible colour combinations if there aren't that many of them.

  3. #3
    Unregistered
    Guest
    Code:
    class winStrct_T 
    {
      public:
        struct COLOR{
          int r;
          int g;
          int b;
        } BLACK, WHITE, GREEN, BLUE;
        int set_colours(int color, int r, int g, int b);
      protected:
        int tmpVar;		
    }winStrct;
    
    int winStrct_T::set_colours(int color, int r, int g, int b)
    {
      switch(color){
        case 1:
          //set BLACK
          break;
        case 2:
          //set WHITE
          break;
        case 3:
          //set GREEN
          break;
        case 4:
          //set BLUE
          break;
        default:
          return EXIT_FAILURE;
      }
      return EXIT_SUCCESS;
    }
    Don't use four different structs, it just takes up unneeded space. Instead create a struct with a generic name and create four instances of it, one for each color. Then you can set the colors for each instance by taking a parameter to set_colours and using a switch to determine which instance to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct accessing member question
    By neesh in forum C Programming
    Replies: 3
    Last Post: 07-30-2008, 02:02 PM
  2. Simple Half-Life Coding Question
    By bengreenwood in forum Game Programming
    Replies: 1
    Last Post: 11-07-2007, 02:18 PM
  3. Vector of structures
    By Highland Laddie in forum C++ Programming
    Replies: 6
    Last Post: 09-13-2005, 09:22 PM
  4. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 03:02 PM
  5. continue with structures and such
    By sballew in forum C Programming
    Replies: 5
    Last Post: 11-04-2001, 05:48 PM