C++ 操作符重载

这里记录一些常见的操作符重载。首先是下标操作符:

头文件定义如下:

 1 #pragma once
 2 #ifndef __FOO__
 3 #define __FOO__
 4 #include <vector>
 5 using namespace std;
 6 
 7 class Foo
 8 {
 9 public:
10     int &operator[](const size_t t);
11     const int &operator[](const size_t t) const;
12     Foo(vector<int> vect):data(vect) {}
13 
14 private:
15     vector<int> data;
16 };
17 
18 #endif // !__FOO__
View Code

 

 代码定义如下:

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <string>
 4 #include "Foo.h"
 5 
 6 using namespace std;
 7 
 8 
 9 int& Foo::operator[](const size_t index)
10 {
11     cout << data[index] << " is returned." << endl;
12     return data[index];
13 }
14 
15 const int& Foo::operator[](const size_t index) const
16 {
17     cout << data[index] << " is returned." << endl;
18     return data[index];
19 }
View Code

 

main 方法定义如下:

 1 // ConsoleApplication23.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <vector>
 6 #include "Foo.h"
 7 #include <string>
 8 #include <iostream>
 9 
10 using namespace std;
11 
12 
13 int main()
14 {
15     vector<int> vect;
16     for (int i = 0; i < 100; ++i)
17     {
18         vect.push_back(i);
19     }
20     Foo foo(vect);
21     foo[1] = 100;
22     cout << foo[1] << endl;
23 
24     system("PAUSE");
25     return 0;
26 }
View Code

 

 

其他常见的如下:

头文件定义:

 1 #pragma once
 2 #ifndef __CHECKOUTRECORD__
 3 #define __CHECKOUTRECORD__
 4 #include <iostream>
 5 #include <vector>
 6 
 7 class CheckOutRecord
 8 {
 9 
10     friend std::ostream& operator<<(std::ostream &cout, CheckOutRecord record);
11     friend std::istream& operator >> (std::istream &in, CheckOutRecord &record);
12     friend CheckOutRecord operator + (CheckOutRecord &record1, CheckOutRecord &record2);
13     friend bool operator == (const CheckOutRecord &record1, const CheckOutRecord &record2);
14     friend bool operator != (const CheckOutRecord &record1, const CheckOutRecord &record2);
15 
16 
17 public:
18     CheckOutRecord(double id, std::string t) :book_id(id), title(t) {}
19     CheckOutRecord& operator=(const CheckOutRecord&);
20     
21 
22 private:
23     double book_id;
24     std::string title;
25 };
26 
27 
28 #endif // !__CHECKOUTRECORD__
View Code

 

代码定义如下:

 1 #include "stdafx.h"
 2 
 3 #include <iostream>
 4 #include "CheckOutRecord.h"
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 ostream& operator<<(ostream &cout, CheckOutRecord record)
10 {
11     
12     cout << record.book_id << " " << record.title;
13     return cout;
14 }
15 
16 istream& operator >> (istream &in, CheckOutRecord &record)
17 {
18     cout << "it is ok? " << in.good() << endl;
19     if (in.good())
20     {
21         in >> record.book_id >> record.title;
22     }
23     else
24     {
25         record = CheckOutRecord(0,"");
26     }
27     return in;
28 }
29 
30 CheckOutRecord operator + (CheckOutRecord &record1, CheckOutRecord &record2)
31 {
32     
33     CheckOutRecord record(0,"");
34     record.book_id = record1.book_id + record1.book_id;
35     record.title = record1.title + record2.title;
36     return record;
37 }
38 
39 bool operator == (const CheckOutRecord &record1, const CheckOutRecord &record2)
40 {
41     return record1.book_id == record2.book_id && record1.title == record2.title;
42 }
43 
44 bool operator != (const CheckOutRecord &record1, const CheckOutRecord &record2)
45 {
46     return record1.book_id != record2.book_id && record1.title != record2.title;
47 }
48 
49 CheckOutRecord& CheckOutRecord::operator=(const CheckOutRecord& record)
50 {    
51     cout << "coming into the operator=" << endl;
52     book_id = record.book_id;
53     title = record.title;
54     return *this;
55 }
View Code

 

main 方法如下:

 1 // ConsoleApplication22.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "CheckOutRecord.h"
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     CheckOutRecord record(1.23,"HelloWorld");
13     cout << record << endl;
14 
15     CheckOutRecord record2(1.23,"HelloWorld");
16     cin >> record2;
17     cout << record2 << endl;
18 
19     CheckOutRecord record3 = record + record2;
20     cout << record3 << endl;
21 
22     CheckOutRecord record4(1,"1");
23     record4 = record2;
24 
25     cout << record4 << endl;
26 
27     cout << (record == record2) << endl;
28     cout << (record != record2) << endl;
29 
30     system("PAUSE");
31     return 0;
32 }
View Code

 

函数对象--调用操作符重载

遇到一个奇怪问题,在如下的定义中:

 1 #pragma once
 2 #ifndef __FUNCTION__
 3 #define __FUNCTION__
 4 
 5 class Func
 6 {
 7 public:
 8     int operator()(int val);
 9 private:
10 };
11 #endif // !__FUNC__
View Code

 

实现如下:

1 #include "stdafx.h"
2 #include "Func.h"
3 
4 
5 int Func::operator()(int val)
6 {
7     return val < 0 ? -val : val;
8 }
View Code

 

main 如下:

 1 // ConsoleApplication24.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "Func.h"
 6 #include <iostream>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     int i = -1;
13     Func func;
14     cout << func(i) << endl;
15     system("PAUSE");
16     return 0;
17 }
View Code

 

执行完一直报错如下,提示我定义的class 不是类或命名空间。

 

最后发现是头文件的声明中,不能define __FUNCTION__ 。这也太巧合了,重新定义一下define __FUNC__ 即可。这里的Func 对象可以通过() 操作符直接调用,尽管是对象,但也可以调用,所以叫做函数对象。

posted on 2017-03-18 10:25  ^~~^  阅读(344)  评论(0编辑  收藏  举报

导航