c++成员运算符的重载
下面是自己编写的一个例子
- #include <string>
- #include <iostream>
- #include <memory>
- using namespace std;
-
- class B
- {
- public:
- B() = default;
- B(const string &str):arr({"sss1","sss2","sss3","sss4"}),curr(0){}
- string & operator*()
- {
- return arr[curr];
- }
- string * operator->()
- {
- return & this->operator*();
- }
- private:
- size_t curr = 0;
- string arr[100] = {"str1","str2","str3","str4"};
- };
-
-
-
- class A
- {
- public:
- A() = default;
- A(const string & str)
- {
- for(int i = 0;i != 100; ++i)
- {
- bind[i] = B("ok");
- }
- }
-
- B& operator*()
- {
- return bind[curr];
-
- }
- B& operator->()
- {
- return operator*();//这里因为operator*返回对象
- }
- private:
- size_t curr = 0;
- B bind[100];
- };
-
-
-
- int main()
- {
- A a;
- cout << a->size() << endl;
-
-
- return 0;
- }
推荐阅读