对下标运算符"[]",既可重载为类成员函数,又可重载为友元函数。
时间: 2023-06-26 12:05:18 浏览: 48
是的,下标运算符"[]"是可以重载为类的成员函数或友元函数的,具体选择哪种方式取决于设计者的需求和设计目的。
当下标运算符被重载为类的成员函数时,它可以访问类的私有成员变量,并且可以使用this指针来访问当前对象。例如:
```cpp
class MyClass {
public:
int operator[](int index) const {
return data[index];
}
private:
int data[10];
};
```
当下标运算符被重载为友元函数时,它不能直接访问类的私有成员变量,但可以通过传入类的对象引用来实现访问私有成员变量的目的。例如:
```cpp
class MyClass {
public:
friend int operator[](const MyClass& obj, int index) {
return obj.data[index];
}
private:
int data[10];
};
```
无论采用哪种方式,重载下标运算符都可以使得类的对象可以像数组一样进行访问。
相关问题
c++运算符重载成员函数和友元函数引用
C++运算符重载可以通过成员函数和友元函数两种方式实现。其中,成员函数重载运算符是将运算符重载为类的成员函数,而友元函数重载运算符是将运算符重载为非类的成员函数。
成员函数重载运算符的语法格式如下:
```cpp
返回类型 operator 运算符(参数列表) {
// 函数体
}
```
例如,将运算符“+”重载为my_string类的成员函数:
```cpp
class my_string {
public:
my_string operator+(const my_string& str) {
my_string result;
// 实现字符串拼接
return result;
}
};
```
而友元函数重载运算符的语法格式如下:
```cpp
friend 返回类型 operator 运算符(参数列表);```
例如,将运算符“+”重载为非my_string类的友元函数:
```cpp
class my_string {
public:
friend my_string operator+(const my_string& str1, const my_string& str2) {
my_string result;
// 实现字符串拼接
return result;
}
};
```
需要注意的是,成员函数重载运算符只能重载部分运算符,例如“+”、“-”、“*”、“/”等,而不能重载赋值运算符“=”和下标运算符“[]”;而友元函数重载运算符可以重载所有运算符。
定义异或加密类,成员重载赋值运算符、下标运算符。友元重载加法和减法运算符
以下是异或加密类的定义,包括成员重载赋值运算符、下标运算符和友元重载加法和减法运算符的实现:
```c++
#include <iostream>
#include <string>
class XorEncryption {
public:
XorEncryption() {}
XorEncryption(const std::string& key) : key_(key) {}
XorEncryption& operator=(const XorEncryption& other) {
if (this != &other) {
key_ = other.key_;
}
return *this;
}
char& operator[](size_t index) {
return key_[index];
}
friend XorEncryption operator+(const XorEncryption& lhs, const XorEncryption& rhs) {
XorEncryption result(lhs.key_);
for (size_t i = 0; i < result.key_.size(); ++i) {
result.key_[i] ^= rhs.key_[i % rhs.key_.size()];
}
return result;
}
friend XorEncryption operator-(const XorEncryption& lhs, const XorEncryption& rhs) {
XorEncryption result(lhs.key_);
for (size_t i = 0; i < result.key_.size(); ++i) {
result.key_[i] ^= rhs.key_[i % rhs.key_.size()];
}
return result;
}
private:
std::string key_;
};
```
在上面的实现中,我们定义了一个名为 `XorEncryption` 的类,用于实现异或加密功能。类中包括一个私有成员变量 `key_`,用于存储加密密钥。在构造函数中,我们可以传入一个字符串类型的密钥。为了方便使用,我们还重载了赋值运算符 `operator=` 和下标运算符 `operator[]`。其中,下标运算符返回密钥中指定位置的字符。
此外,我们还实现了两个友元函数,用于重载加法和减法运算符。在这两个函数中,我们首先定义了一个 `XorEncryption` 类型的变量 `result`,并将其初始化为左操作数的密钥。然后,我们使用异或运算符对右操作数的密钥进行加密或解密操作,并将结果保存到 `result.key_` 中。最后,我们返回 `result` 变量。
使用上述定义的 `XorEncryption` 类,我们可以对字符串进行加密和解密操作,例如:
```c++
int main() {
XorEncryption key("secret");
XorEncryption encrypted = key + "Hello, world!";
std::cout << encrypted[0] << encrypted[1] << encrypted[2] << std::endl; // output: 1AM
XorEncryption decrypted = encrypted - key;
std::cout << decrypted << std::endl; // output: Hello, world!
return 0;
}
```
在上面的代码中,我们首先定义了一个 `XorEncryption` 类型的变量 `key`,并将其初始化为字符串 `"secret"`。然后,我们使用加法运算符将字符串 `"Hello, world!"` 加密,并将结果保存到 `encrypted` 变量中。接着,我们使用下标运算符访问 `encrypted` 变量的前三个字符,输出结果为 `"1AM"`。最后,我们使用减法运算符对 `encrypted` 变量进行解密操作,并将结果保存到 `decrypted` 变量中。最终,我们输出 `decrypted` 变量的值,即原始字符串 `"Hello, world!"`。