Special variables: Difference between revisions

C++ entry
(Added 11l)
(C++ entry)
Line 515:
 
Furthermore one could consider <code>errno</code> from <code><errno.h></code> as a special variable although it actually is a macro which expands to an modifiable lvalue of type <code>int</code>. Many library functions set it to a positive value in case of an error.
 
=={{header|C++}}==
 
Besides <code>errno</code> like C, C++ has the <code>this</code> pointer so objects can refer to themselves.
 
<lang cpp>#include <iostream>
 
struct SpecialVariables
{
int i = 0;
 
SpecialVariables& operator++()
{
// 'this' is a special variable that is a pointer to the current
// class instance. It can optionally be used to refer to elements
// of the class.
this->i++; // has the same meaning as 'i++'
 
// returning *this lets the object return a reference to itself
return *this;
}
 
};
 
int main()
{
SpecialVariables sv;
auto sv2 = ++sv; // makes a copy of sv after it was incremented
std::cout << " sv :" << sv.i << "\n sv2:" << sv2.i << "\n";
}</lang>
{{out}}
<pre>
sv :1
sv2:1
</pre>
 
=={{header|Clojure}}==
125

edits