// simple Int class with ctor, increment operator and ostream overloadLink: Περισσότερα για την διαφορά prefix/suffix.
class Int{
public:
Int& operator++();
const Int operator++( int n );
Int(int i): _i(i) { };
friend ostream& operator <<(ostream& os, const Int &i);
private:
int _i;
};
// look difference between return values: Int& (in prefix)
Int& Int::operator++() {
_i++; // Handle case where no argument is passed.
return *this;
}
// and const Int (in postfix)
const Int Int::operator++( int n ) {
Int tmp(*this);
if( n != 0 )
for (int nr=0; nr<n; ++nr, ++(*this));
else
++(*this);
return tmp;
}
// overloading << operator for Int class
ostream& operator <<(ostream& os, const Int &i) {
os << i._i;
return os;
}
void main() {
Int gg(0);
cout << gg.operator++(25) << endl ; // Increment by 25 but shown 0
cout << gg << endl; // in next reference is shown as 25
// gg = 25
cout << gg++ << endl; // gg is 25. Only in the next reference will it be 26
cout << gg << endl; // gg is 26.
// gg = 26
cout << ++gg << endl; // ggia is immidiately 27
cout << gg << endl; // ggia is 27
return;
}
Programming links:
20071116
C++: Example of class with ctor, increment operator (prefix and suffix) and ostream overloading.
Εγγραφή σε:
Σχόλια ανάρτησης (Atom)
Πληροφορίες
Αρχειοθήκη ιστολογίου
-
►
2010
(5)
- ► Σεπτεμβρίου (1)
- ► Φεβρουαρίου (2)
-
►
2008
(14)
- ► Δεκεμβρίου (3)
- ► Φεβρουαρίου (1)
- ► Ιανουαρίου (5)
-
▼
2007
(40)
- ► Δεκεμβρίου (4)
-
▼
Νοεμβρίου
(11)
- Newline terminator in Unix/Linux, Windows and Mac ...
- Convert a tab delimited file to CSV format using R...
- Remove all "^M" characters from a file using vi.
- Convert all new lines to tab delimited using regex...
- Regular expressions links/briefly.
- Λεξικά για Firefox/Thunderbird, Open Office για Ελ...
- C++: Defining/initializing static member(s) within...
- C++: Example of class with ctor, increment operato...
- C++: difference between prefix and suffix incremen...
- Visual Studio .NET 2005: Static library creation/u...
- C++ Boost: Reading all the text files of directory...
Ετικέτες
- C++ (25)
- Unix/Linux (9)
- SuSE 10.2 (8)
- boost (7)
- regex (6)
- Windows (5)
- Windows Vista (4)
- functions (4)
- vim (4)
- Visual Studio .NET 2005 (3)
- firefox (3)
- Windows 7 (2)
- asus eee 901 (2)
- open office (2)
- thunderbird (2)
- video tools (2)
- LaTeX (1)
- Praat Script (1)
- adobe cs4 (1)
- apache (1)
- files (1)
- laptop repair (1)
- php/MySQL (1)
- ubuntu 10.04 (1)
- ubuntu 12.04 (1)
- ubuntu 9.04 (1)
- ubuntu eee (1)
- western digital (1)
- wordpress (1)

Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου