Sign in to follow this  

Interesting Syntax

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
while were on this topic what the hell is this:

const CVector operator | (const scalar_t length) const
{
return *this * (length / !(*this));
}

Share this post


Link to post
Share on other sites
I say 3.

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

Share this post


Link to post
Share on other sites
grid is a pointer to a 4x4 array of bool pointers, e.g.

bool (*grid)[4][4];
bool agrid[4][4];

grid = &agrid;




Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
How about this one:

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

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


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :

Share this post


Link to post
Share on other sites

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this