operator SDL_Color 0.o?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

operator SDL_Color 0.o?

Post by XianForce »

Well I've been reading through a copy of Focus on SDL that I bought from Amazon (Yay for used books?) and I was looking through the Framework stuff and saw that in the abstraction of SDL_Color. Unfortunately I don't have the CD (Down-Side of used books =/ ) and can't seek further understanding there.

From my perspective, when I see that operator keyword, my brain instantly thinks +, -, /, *, =, ==, +=, etc.

So first of all, what does operator SDL_Color() and operator SDL_Color*() mean, and how would I use them?
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: operator SDL_Color 0.o?

Post by RyanPridgeon »

It's not an operator, its a data structure. It contains a value for red, green, blue, and alpha(unused).

This is just a way to represent any colour, rather than having format dependent (such as indexed colour or 16 bit colour) colour data. That's why it's called abstraction.

Because it's a C struct, you can treat it as 32 bit colour data too. For example you could pass 0xFF00FF00 for a pink colour.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: operator SDL_Color 0.o?

Post by XianForce »

RyanPridgeon wrote:It's not an operator, its a data structure. It contains a value for red, green, blue, and alpha(unused).

This is just a way to represent any colour, rather than having format dependent (such as indexed colour or 16 bit colour) colour data. That's why it's called abstraction.

Because it's a C struct, you can treat it as 32 bit colour data too. For example you could pass 0xFF00FF00 for a pink colour.
I know what an SDL_Color is... But in the Focus On SDL framework, it's stated like this:

operator SDL_Color()
and
operator SDL_Color*()

I want to know why a data structure is used as an overloaded operator, and how it would be used.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: operator SDL_Color 0.o?

Post by programmerinprogress »

you can't make your own operators, you can only overload the pre-defined operators laid out by the language as far as I'm aware.

This sounds like a misprint, but I've never read the book.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: operator SDL_Color 0.o?

Post by andrew »

They're conversion operators:

Code: Select all

//conversion operators
CColor::operator SDL_Color()
{
	//return color
	return(m_Color);
}

CColor::operator SDL_Color*()
{
	//return pointer to color
	return(&m_Color);
}
Check it out.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: operator SDL_Color 0.o?

Post by XianForce »

andrew wrote:They're conversion operators:

Code: Select all

//conversion operators
CColor::operator SDL_Color()
{
	//return color
	return(m_Color);
}

CColor::operator SDL_Color*()
{
	//return pointer to color
	return(&m_Color);
}
Check it out.
Thanks so much. I've been looking around my room for the disk, but I don't even remember if I ever had it. Couldn't find it on Google either, kept finding the ebook through a google search haha.

Well that's pretty nifty, you don't call the operators, it's just an automatic conversion =D.

EDIT: One more quick question.

Does this mean, I could just pass in a CColor object into any function asking for a SDL_Color, and it will do the necessary conversions? Or Would I have to do something like: CColorInstance->getColor()?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: operator SDL_Color 0.o?

Post by Ginto8 »

XianForce wrote:
andrew wrote:They're conversion operators:

Code: Select all

//conversion operators
CColor::operator SDL_Color()
{
	//return color
	return(m_Color);
}

CColor::operator SDL_Color*()
{
	//return pointer to color
	return(&m_Color);
}
Check it out.
Thanks so much. I've been looking around my room for the disk, but I don't even remember if I ever had it. Couldn't find it on Google either, kept finding the ebook through a google search haha.

Well that's pretty nifty, you don't call the operators, it's just an automatic conversion =D.

EDIT: One more quick question.

Does this mean, I could just pass in a CColor object into any function asking for a SDL_Color, and it will do the necessary conversions? Or Would I have to do something like: CColorInstance->getColor()?
almost. You could simply pass a CColor for a function asking for a SDL_Color, howerver, this will probably, if the compiler isn't too smart (or is smart, idk how these things work all the time), be a dynamic cast, which is slower (I think) and can have unexpected behavior (again, I'm not quite sure). The better thing would be to use a static cast, which can be done one of two ways:

This is the C-style way of doing it (I prefer this style):

Code: Select all

CColor color;
func((SDL_Color)color);
And this is the way introduced in C++:

Code: Select all

CColor color;
func(static_cast<SDL_Color>(color));
either way is fine, and it shouldn't cause any problems that dynamic casts (could) cause. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: operator SDL_Color 0.o?

Post by XianForce »

Ginto8 wrote:
XianForce wrote:
andrew wrote:They're conversion operators:

Code: Select all

//conversion operators
CColor::operator SDL_Color()
{
	//return color
	return(m_Color);
}

CColor::operator SDL_Color*()
{
	//return pointer to color
	return(&m_Color);
}
Check it out.
Thanks so much. I've been looking around my room for the disk, but I don't even remember if I ever had it. Couldn't find it on Google either, kept finding the ebook through a google search haha.

Well that's pretty nifty, you don't call the operators, it's just an automatic conversion =D.

EDIT: One more quick question.

Does this mean, I could just pass in a CColor object into any function asking for a SDL_Color, and it will do the necessary conversions? Or Would I have to do something like: CColorInstance->getColor()?
almost. You could simply pass a CColor for a function asking for a SDL_Color, howerver, this will probably, if the compiler isn't too smart (or is smart, idk how these things work all the time), be a dynamic cast, which is slower (I think) and can have unexpected behavior (again, I'm not quite sure). The better thing would be to use a static cast, which can be done one of two ways:

This is the C-style way of doing it (I prefer this style):

Code: Select all

CColor color;
func((SDL_Color)color);
And this is the way introduced in C++:

Code: Select all

CColor color;
func(static_cast<SDL_Color>(color));
either way is fine, and it shouldn't cause any problems that dynamic casts (could) cause. ;)
Okay, thanks =D.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: operator SDL_Color 0.o?

Post by qpHalcy0n »

Ya got it the other way around. A static cast would be "as close" to what is actually happening. Dynamic casts only deal w/ ptr or reference types, and even there, there's no way for the language to ensure that it's a safe operation. So its up to the programmer on that one. This is the basis for a type of RTTI is it's "elegant" handling of failure to convert a class because it's not a "complete class" of what you're attempting to convert it into.

However, the static cast is just an implicit form (in C++) of just a a regular ole' typecast. For obvious reasons, in C++ dynamic casting and static casting had to be resolved explicitly because dynamic casts only deal w/ ptr or reference types in regards to polymorphic classes (this is why it's said to be "slow").

In this particular case, the cast is defined by the operator and is perfectly safe and presumably fast (this is a very trivial casting).
Post Reply