Skip to main content
GameDev.net gamedev.net
🔒 Locked

Interesting Syntax

Started by Phineas Jan 20, 2006 at 6:26 AM 17 replies 1.7k views
Original Post
Phineas
Phineas
Please explain the syntax of the following: bool (*grid)[4][4]; I imagine two possibilities: 1. grid is a pointer to 4x4 array of boolean values. 2. grid is a 4x4 array of bool pointers. 3. grid is a pointer to an array of bool pointers, each pointing to an array of 4 boolean values. Thanks for your help.
ForeverNoobie
ForeverNoobie
while were on this topic what the hell is this:
const CVector operator | (const scalar_t length) const{       return *this * (length / !(*this));}
Simplicity is the ultimate sophistication. – Leonardo da Vinci
spoulson
spoulson
I say 3.

Forevernoob, you could at least wait for Phineas to get an answer before hijacking his thread.
darookie
darookie
grid is a pointer to a 4x4 array of bool pointers, e.g.
bool (*grid)[4][4];bool agrid[4][4];grid = &agrid



ForeverNoobie
ForeverNoobie
Quote:
Original post by spoulson
I say 3.

Forevernoob, you could at least wait for Phineas to get an answer before hijacking his thread.


I didnt mean to hijack, sory. Anyways afew months ago someone told me I must understand that code before i could join their team and I still have no clue what the hell it is.
Simplicity is the ultimate sophistication. – Leonardo da Vinci
darookie
darookie
Quote:
Original post by ForeverNoobie
while were on this topic what the hell is this:
const CVector operator | (const scalar_t length) const{       return *this * (length / !(*this));}

Simple:
*this dereferences "this" and enables the use of the class' overloaded operators without using "->".
If you don't dereference "this", you'd have to use the following syntax to invoke the overloaded operators:
const CVector operator | (const scalar_t length) const{       return this->operator * ( length / this->operator !( ) );}

Your code sample, btw. messes the operator semantics up in a horrible way[smile]. If you mean what the code does - it sets the length of CVector to the value specified by the "length" parameter (assuming the unary ! operator returns the length of the vector and "*" denotes scalar multiplication).

Cheers,
Pat.
WillC
WillC
Quote:
Original post by ForeverNoobie
while were on this topic what the hell is this:
const CVector operator | (const scalar_t length) const{       return *this * (length / !(*this));}


I'd say it's a good example of what Not to do with operator overloading.

It looks like it sets the length of a vector to the value specified, by scaling it. The assumption with this though is that the ! operator has been overloaded to return the current vector length.

In my opinion though this kind of operator overloading is a bad coding technique as it's so ambiguous. Much better to have a properly named function to do that same thing.

To be honest, I recon you're better off not joining the team that wrote that anyway :)
Sharlin
Sharlin
Woah, that's one hell of a WTF. Code like that is why some (mostly Java) people insist that operator overloading is an unconditionally bad feature.
rip-off
rip-off
Quote:
Original post by Sharlin
Woah, that's one hell of a WTF. Code like that is why some (mostly Java) people insist that operator overloading is an unconditionally bad feature.


its just the use of operator!() that is confusing. the others( operator*, operator/ ) are okay.
Sharlin
Sharlin
Well, using operator| to scale a vector isn't too obvious either.
rip-off
rip-off
Quote:
Original post by Sharlin
Well, using operator| to scale a vector isn't too obvious either.


yeah i suppose i missed that. i was only looking at the code inside the function.

yeah, thats bad alright...
socrates200X
socrates200X
const CVector operator | (const scalar_t length) const
{
return *this * (length / !(*this));
}

Does this even compile? That is, is it valid to use a this pointer if the method isn't within a class, like: "const CVector CVector::operator | ..."? If it is valid, then what exactly is this this-ing?
Roboguy
Roboguy
Quote:
Original post by Sharlin
Woah, that's one hell of a WTF. Code like that is why some (mostly Java) people insist that operator overloading is an unconditionally bad feature.


Operator overloading (and even the ability to define new operators, which you can't really do in C++) has it's uses, but it can be abused very badly.
JohnBolton
JohnBolton
Quote:
Original post by socrates200X
...Does this even compile?

It's probably a typo or defined inside the class -- otherwise it won't compile for a couple reasons. As you note, it should then be "CVector::operator |"

BTW, one reason for not using '|' for multiplication is that the precedence is wrong. What is the value of c below?
    CVector a( 1, 1, 1 );    CVector b( 2, 2, 2 );    CVector c = a + b|3; 
If you think it is [7, 7, 7], you are the victim of poorly designed operator overloading.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Kitt3n
Kitt3n
How about this one:

char (*(*x())[])()

is a function returning a pointer to an array of pointers
to functions returning char


visit my website at www.kalmiya.com
Sharlin
Sharlin
Quote:
Original post by JohnBolton
Quote:
Original post by socrates200X
...Does this even compile?

It's probably a typo -- otherwise it won't compile for a couple reasons. As you note, it should be"CVector::operator |"


Not necessarily - it could be (and probably is) written inline inside the class declaration.

Zahlman
Zahlman
Quote:
Original post by JohnBolton
Quote:
Original post by socrates200X
...Does this even compile?

It's probably a typo or defined inside the class -- otherwise it won't compile for a couple reasons. As you note, it should then be "CVector::operator |"

BTW, one reason for not using '|' for multiplication is that the precedence is wrong. What is the value of c below?
    CVector a( 1, 1, 1 );    CVector b( 2, 2, 2 );    CVector c = a + b|3; 
If you think it is [7, 7, 7], you are the victim of poorly designed operator overloading.


They're not using it for multiplication but for normalization, if I understood it correctly. In which case they probably do want an operator with lower precedence than addition :
b2b3
b2b3
Quote:
Original post by SamLowry
The future lies in whitespace overloading.


That's the most complicated April Fools joke I have ever seen [lol].

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.