See also
http://stackoverflow.com/questions/2476381/c-constructor-initialization-list-strangeness
That introduces "member initialization list".
You have
1 2 3 4 5
|
Test (int x = 0, int y = 0)
{
this->x = x;
this->y = y;
}
| |
Which is close to
1 2 3 4 5 6 7
|
Test (int x = 0, int y = 0)
: x(), y() // initialization
{
// assignment
this->x = x;
this->y = y;
}
| |
But why initialize and assign, when mere initialization is sufficient?
1 2 3 4
|
Test (int x = 0, int y = 0)
: x(x), y(y) // initialization
{
}
| |
The link explain why that is not ambiguous, but one could use other names for "clarity":
1 2 3 4
|
Test (int a = 0, int b = 0)
: x(a), y(b) // initialization
{
}
| |
A pointer is a variable that stores a value (just like all variables).
The value stored by pointer is a memory address.
The pointer has (unary) dereference operator
*
that is used to access the object (variable) that is at the memory address.
A pointer can have a memory address that does not contain object of correct type. Such pointer has invalid value and a dereference leads to undefined result.
Memory address 0 is always invalid. Easy to test for.
nullptr
is a named constant that has type pointer and value 0.
Python has equivalent of 'this'. It is more explicit and is named 'self'.
http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self-in-python
Only members of class can access the private members of the class. A struct, by default, has public members. Therefore, as an example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
struct Foo {
int x;
void setX( int x ) { this->x = x; }
};
void setX( Foo * this, int x ) { this->x = x; }
int main() {
Foo obj;
obj.setX( 7 ); // member function
setX( &obj, 42 ); // stand-alone function
return 0;
}
| |
Do you notice the similarity of the two approaches?
The C++ is based on C and C has structs, but no member functions. C has to use stand-alone functions for everything. C++ has member functions in order to implement access control, and it implicitly provides the 'this' for them. Granted, not every member function needs the this.
What exactly is (*this) in &setX and &setY referring to? |
The liberal use of whitespace ...
Not "in &setX and &setY". The names of the functions are setX and setY, or to be more precise Test::setX(int) and Test::setY(int).
1 2 3 4 5 6 7 8 9 10 11 12
|
Test &setX(int a);
// is same as
Test&setX(int a);
// is same as
Test& setX(int a);
// is same as
Test & setX(int a);
// is same as
Test & setX (int a);
// is same as
Test &
setX(int a);
| |
In every case, one should read (approximately) from right to left:
The 'setX' is a name of a function that takes one integer argument by value and returns a reference to an object that has type Test.
The amount of whitespace (when not syntactically critical) is a matter of style.