operator SDL_Color 0.o?
Moderator: Coders of Rage
operator SDL_Color 0.o?
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?
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?
- RyanPridgeon
- 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?
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.
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.
Re: operator SDL_Color 0.o?
I know what an SDL_Color is... But in the Focus On SDL framework, it's stated like this: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.
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.
- programmerinprogress
- 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?
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.
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
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
Re: operator SDL_Color 0.o?
They're conversion operators:
Check it out.
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);
}
Re: operator SDL_Color 0.o?
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.andrew wrote:They're conversion operators:Check it out.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); }
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()?
- Ginto8
- 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?
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:XianForce wrote: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.andrew wrote:They're conversion operators:Check it out.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); }
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()?
This is the C-style way of doing it (I prefer this style):
Code: Select all
CColor color;
func((SDL_Color)color);
Code: Select all
CColor color;
func(static_cast<SDL_Color>(color));
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.
Re: operator SDL_Color 0.o?
Okay, thanks =D.Ginto8 wrote: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:XianForce wrote: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.andrew wrote:They're conversion operators:Check it out.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); }
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()?
This is the C-style way of doing it (I prefer this style):And this is the way introduced in C++:Code: Select all
CColor color; func((SDL_Color)color);
either way is fine, and it shouldn't cause any problems that dynamic casts (could) cause.Code: Select all
CColor color; func(static_cast<SDL_Color>(color));
-
- Respected Programmer
- Posts: 387
- Joined: Fri Dec 19, 2008 3:33 pm
- Location: Dallas
- Contact:
Re: operator SDL_Color 0.o?
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).
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).