操作符過載集錦
在C++中約定,對於++操作符過載 ,將不帶引數的作為前項++,將帶有一個int型別亞引數的過載作為後項++。
#include <iostream>
using namespace std;
class Data
{
private:
int d;
public:
Data(void) : d(0)
{
}
Data(int i) : d(i)
{
}
Data(const Data& data) : d(data.d)
{
}
Data& operator = (const int i)
{
d = i;
return *this;
}
Data& operator = (const Data& data)
{
d = data.d;
return *this;
}
bool operator == (const int i)const
{
return d == i;
}
bool operator == (const Data & data)const
{
return data.d == d;
}
operator int(void)const
{
return d;
}
Data operator + (int i)const
{
return Data(d + i);
}
Data& operator += (int i)
{
d += i;
return *this;
}
Data& operator += (const Data& data)
{
d += data.d;
return *this;
}
Data operator << (const int i)const
{
return Data(d << i);
}
Data& operator <<= (const int i)
{
d <<= i;
return *this;
}
Data operator >> (const int i)const
{
return Data(d >> i);
}
Data& operator >>= (const int i)
{
d >>= i;
return *this;
}
Data* operator -> (void)
{
d--;
return this;
}
Data& operator * (int i)
{
d *= i;
return *this;
}
Data& operator * (const Data& data)
{
d *= data.d;
return *this;
}
void operator() (void)
{
cout << "This is a data!" << endl;
}
friend ostream& operator << (ostream& out, const Data* data);
friend int operator + (int i, const Data& data);
int GetData(void)const
{
return d;
}
};
int operator + (int i, const Data& data)
{
return i + data.d;
}
ostream& operator << (ostream& out, const Data* data)
{
out << data->d + 100 << endl;
return out;
}
class Number
{
private:
Data *pData;
int n;
public:
explicit Number(void) : pData(NULL), n(0)
{
}
explicit Number(Data *p) : pData(p), n(1)
{
}
Data& operator * (void) const
{
return *pData;
}
int GetData(void) const
{
return n;
}
};
int main(void)
{
cout << "The answer is: " << 3 + Data(5) << endl;
Data d(5);
cout << d << endl;
cout << &Data(10) << endl;
cout << "Hey, " << Data(0)->GetData() << endl;
Data d2 = 3 + d - 4;
cout << "Hoo, " << d2->GetData() << endl;
Data *p = &d;
d = d * d2;
cout << "The data is: " << (*p).GetData() << endl;
d();
Number n(&d);
cout << "The answer is: " << (*n).GetData() << endl;
cout << "The answer is: " << n.GetData() << endl;
return 0;
}
using namespace std;
class Data
{
private:
int d;
public:
Data(void) : d(0)
{
}
Data(int i) : d(i)
{
}
Data(const Data& data) : d(data.d)
{
}
Data& operator = (const int i)
{
d = i;
return *this;
}
Data& operator = (const Data& data)
{
d = data.d;
return *this;
}
bool operator == (const int i)const
{
return d == i;
}
bool operator == (const Data & data)const
{
return data.d == d;
}
operator int(void)const
{
return d;
}
Data operator + (int i)const
{
return Data(d + i);
}
Data& operator += (int i)
{
d += i;
return *this;
}
Data& operator += (const Data& data)
{
d += data.d;
return *this;
}
Data operator << (const int i)const
{
return Data(d << i);
}
Data& operator <<= (const int i)
{
d <<= i;
return *this;
}
Data operator >> (const int i)const
{
return Data(d >> i);
}
Data& operator >>= (const int i)
{
d >>= i;
return *this;
}
Data* operator -> (void)
{
d--;
return this;
}
Data& operator * (int i)
{
d *= i;
return *this;
}
Data& operator * (const Data& data)
{
d *= data.d;
return *this;
}
void operator() (void)
{
cout << "This is a data!" << endl;
}
friend ostream& operator << (ostream& out, const Data* data);
friend int operator + (int i, const Data& data);
int GetData(void)const
{
return d;
}
};
int operator + (int i, const Data& data)
{
return i + data.d;
}
ostream& operator << (ostream& out, const Data* data)
{
out << data->d + 100 << endl;
return out;
}
class Number
{
private:
Data *pData;
int n;
public:
explicit Number(void) : pData(NULL), n(0)
{
}
explicit Number(Data *p) : pData(p), n(1)
{
}
Data& operator * (void) const
{
return *pData;
}
int GetData(void) const
{
return n;
}
};
int main(void)
{
cout << "The answer is: " << 3 + Data(5) << endl;
Data d(5);
cout << d << endl;
cout << &Data(10) << endl;
cout << "Hey, " << Data(0)->GetData() << endl;
Data d2 = 3 + d - 4;
cout << "Hoo, " << d2->GetData() << endl;
Data *p = &d;
d = d * d2;
cout << "The data is: " << (*p).GetData() << endl;
d();
Number n(&d);
cout << "The answer is: " << (*n).GetData() << endl;
cout << "The answer is: " << n.GetData() << endl;
return 0;
}
相關文章
- C++操作符過載C++
- C++ 操作符過載C++
- 〔轉載〕ABAP常用函式集錦函式
- sqlserver 常用儲存過程集錦SQLServer儲存過程
- PHP 中的操作符過載PHP
- 五月前端知識集錦(每月不可錯過的文章集錦)前端
- 六月前端知識集錦(每月不可錯過的文章集錦)前端
- 四月前端知識集錦(每月不可錯過的文章集錦)前端
- Kotlin——初級篇(五):操作符與操作符過載一Kotlin
- 箴言集錦箴言
- c#一元操作符過載C#
- 技術問答集錦(11)JVM類載入JVM
- 12款CSS3應用集錦下載CSSS3
- C++ operator關鍵字(過載操作符)C++
- 知識集錦
- 面試題集錦面試題
- 網站集錦網站
- 優秀IT書籍下載集錦100本(轉)
- 前端知識集錦前端
- 面試集錦(九)git面試Git
- 面試集錦(十二)hashMap面試HashMap
- docker 踩坑集錦Docker
- flutter問題集錦Flutter
- Git命令集錦Git
- 個人外掛錦集
- kafka問題集錦Kafka
- ios 崩潰集錦iOS
- PHP經驗集錦PHP
- mssql論壇集錦SQL
- java網址集錦Java
- dart基礎之類介面操作符過載相關Dart
- RxJava操作符之過濾操作符(五)RxJava
- JS工程化集錦JS
- 面試集錦(三)spring面試Spring
- vue知識點集錦Vue
- Python例項集錦Python
- iOS 感測器集錦iOS
- PHP命令執行集錦PHP