this-> in class implementation

Hi there,

I'm trying to get an AD9835 DDS work, and I found a nice library in this forum (DDS Library for The AD9835 by LachlanGunn). It's a little bit old, but absolutely usable, except that you have to use ARDUINO.H instead of WProgram.H as far as I could understand.

IN the class conttruction I found the instruction "this->"

AD9835::AD9835(const int pinFSYNC, const int pinSCLK, const int pinSDATA,
const int pinFSEL, const int pinPSEL1, const int pinPSEL0,
const unsigned long hzMasterClockFrequency)
{
// First we initialise our class.
this->pinSCLK = pinSCLK;
this->pinSDATA = pinSDATA;
this->pinFSYNC = pinFSYNC;
this->pinFSEL = pinFSEL;
this->pinPSEL1 = pinPSEL1;
this->pinPSEL0 = pinPSEL0;
this->hzMasterClockFrequency = hzMasterClockFrequency;
}

I was not able to find a definition of "this" in the ARDUINO fuctions ( although I understand the C meaning), and I'm wondering if this still exist or if it is repaced by some other equivalent function.

Thanks for your help.

Gerard.

You can omit the this-> If you have a variable within the object it will naturally take precedence over any global variable of the same name.

OK Ken thanks for the info, but what the heck with these lines?

if you write A=A it's obvious!

Do you mean that it's done to explicitely define the variable inside the function?

although I understand the C meaning

this-> doesn't have a meaning in C. It does in C++.

You can omit the this-> If you have a variable within the object it will naturally take precedence over any global variable of the same name.

But, when you want to explicitly access the member field (not global variable), you need to use the this-> construct.

Of course, having function arguments that have the same name as member fields is something that is very easily avoided, so the need to use this-> is pretty rare. That is certainly the case in OPs snippet.

http://en.wikipedia.org/wiki/Dereference_operator

In C, there is syntactic sugar for accessing members of a struct or union, given a pointer to such. Given a pointer p to a structure s so: *p = s the usual way to access a member a is as s.a which, given the pointer, is expressed as (*p).a or can instead be accessed by the shorthand: p->a This can be chained; for example, in a linked list, one may refer to n->next->next for the second following node (assuming that n->next is not null).

//consider the issue where we have
AD9835 * myClockPointer;

//attempting to access the pinFSYNC we might expect to use
int x=myClockPointer.pinFSYNC;

//but wait, myClockPointer is just a humble pointer!  It doesn't have a member called pinFSYNC
//so instead we have to look for the member of the object that our pointer is pointing to thus
pinFSYNC->pinFSYNC;

The word this, is just a pointer to the current incidence of an object. Since it's a pointer we have to use the -> style

OK, thanks to all. It's clearer now!