Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


typedef, structs, and arrays


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 30 January 2014 - 01:10 AM

Hello, I have a struct that defines an RGBA colour: 

struct Colour{
	float r; 
	float g;
	float b;
	float a;
};

Then I have an array of some predefined colours  so instead of having  to write out something link display.clear(Colour(1.f, 0.f, 0.f, 1.f) I would instead write  display.clear(clr::red); So I have an array of colours: 

//pre defined colours 
Colour colours[6] = {{1.f, 0.f, 0.f, 1.f},
					 {1.f, 0.f, 0.f, 1.f},
					 {1.f, 0.f, 0.f, 1.f},
					 {1.f, 0.f, 0.f, 1.f},
					 {1.f, 0.f, 0.f, 1.f},
					 {1.f, 0.f, 0.f, 1.f}};

Now here is my problem. I want to typedef all the elements in the array so instated of writing clr::colours[0] I write clr::red. SO I tried writing them like this and it did not work: 

namespace clr{
	typedef colours[0] red;
	typedef colours[1] green;
	typedef colours[2] blue;
	typedef colours[3] magenta;
	typedef colours[4] white;
	typedef colours[5] black;
}//namespace clr

Is this possible or did I just do it wrong?  Thanks for any help.



Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 20377

Like
6Likes
Like

Posted 30 January 2014 - 01:24 AM

You don't want a typedef (an alias of a type), but a constant (an "alias", so to speak, of a value).

namespace clr
{
     const Colour red = colours[0];
     const Colour green = colours[1];
}

 
Though, I don't recall whether 'colours[1]' is garunteed to be initialized before 'green' is (might be undefined behavior), so it'd probably be better (and clearer in the code) to do it the other way around:

namespace clr
{
     const Colour red = {1.f, 0.f, 0.f, 1.f};
     const Colour green = {0.f, 1.f, 0.f, 1.f};
 
     //pre defined colours 
     Colour colours[6] = {red,
                          green,
                          etc...};
}

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 30 January 2014 - 02:23 AM

Thanks, I didn't even think of that.



#4 Álvaro   Crossbones+   -  Reputation: 13684

Like
1Likes
Like

Posted 30 January 2014 - 04:23 AM

Though, I don't recall whether 'colours[1]' is garunteed to be initialized before 'green' is (might be undefined behavior), so it'd probably be better (and clearer in the code) to do it the other way around:


Unless you initialize them in different translation units, it should be just fine. I still prefer the other method, or this variation of it:
struct Colour{
  float r, g, b, a;
  
  Colour(float r, float g, float b, float a) : r(r), g(g), b(b), a(a) {
  }
};

namespace clr {
  const Colour red(1.f, 0.f, 0.f, 1.f);
  const Colour green(0.f, 1.f, 0.f, 1.f);
  
  //pre defined colours 
  Colour colours[6] = {red,
                       green,
                       etc...};
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS