const Type& operator[](int id) const const { if(id<0||id>size-1) Error("id is illegal!\n"); return data[id];为什么会两个const
时间: 2024-04-03 14:33:30 浏览: 9
这是因为该函数是一个常量成员函数,它不会修改类的成员变量。第一个 const 保证了 this 指针是一个指向常量的指针,即该函数不能修改对象的成员变量。第二个 const 表示该函数返回的是一个常量引用,即不能通过返回值修改对象的成员变量。这两个 const 都是为了保证函数的只读性,防止函数无意中修改了对象的状态。
相关问题
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <string> using namespace std; template <class T> class myarr { public: myarr(int n) { this->C = n; this->size = 0; this->p = new T[C]; } myarr(const myarr& other) { this->C = other.C; this->size = other.size; this->p = new T[other.C]; for (int i = 0; i < size; i++) p[i] = other[i]; } ~myarr() { if (this->p != NULL) { delete[]this->p; this->p = NULL; } } void push_back(const T& num) { if (this->C == this->size) return; this->p[this->size] = num; this->size++; } void pop_back() { if (this->size == 0) return; this->size--; } myarr& operator=(const myarr& other) { if (this->p != NULL) { delete[]this->p; } this->C = other.C; this->size = other.size; this->p = new T[other.C]; for (int i = 0; i < size; i++) p[i] = other[i]; } T& operator[](int index) { return this->p[index]; } int getC() { return this->C; } int getsize() { return this->size; } private: T* p; int size; int C; }; class base { public: base(); base(string n, int a) { this->age = a; this->name = n; } void show() { cout << "名字: " << name << " 年龄: " << age; } private : string name; int age; }; void myprintf(myarr<base>& m) { for (int i = 0; i < m.getsize(); i++) { m[i].show(); cout << endl; } } void test() { base b1("zhangshan", 100); myarr<base>a(5); for(int i=0;i<5;i++) a.push_back(b1); myprintf(a); } int main() { test(); return 0; }这段代码有什么问题
这段代码有两个问题:
1. 在类myarr的赋值运算符函数中,应该返回*this,即返回当前对象的引用。
2. 在函数myprintf中,应该将参数myarr<base>&改为const myarr<base>&,因为函数并不会修改传入的数组。同时,在函数内部也应该使用const T&遍历数组,以避免不必要的复制。
修改后的代码如下所示:
```
#include<iostream>
#include <string>
using namespace std;
template <class T>
class myarr {
public:
myarr(int n)
{
this->C = n;
this->size = 0;
this->p = new T[C];
}
myarr(const myarr& other)
{
this->C = other.C;
this->size = other.size;
this->p = new T[other.C];
for (int i = 0; i < size; i++)
p[i] = other[i];
}
~myarr()
{
if (this->p != NULL)
{
delete[]this->p;
this->p = NULL;
}
}
void push_back(const T& num)
{
if (this->C == this->size)
return;
this->p[this->size] = num;
this->size++;
}
void pop_back()
{
if (this->size == 0)
return;
this->size--;
}
myarr& operator=(const myarr& other)
{
if (this->p != NULL)
{
delete[]this->p;
}
this->C = other.C;
this->size = other.size;
this->p = new T[other.C];
for (int i = 0; i < size; i++)
p[i] = other[i];
return *this; // 返回当前对象的引用
}
T& operator[](int index)
{
return this->p[index];
}
int getC()
{
return this->C;
}
int getsize()
{
return this->size;
}
private:
T* p;
int size;
int C;
};
class base {
public:
base();
base(string n, int a)
{
this->age = a;
this->name = n;
}
void show() const // 加上const
{
cout << "名字: " << name << " 年龄: " << age;
}
private :
string name;
int age;
};
void myprintf(const myarr<base>& m) // 改为const myarr<base>&
{
for (int i = 0; i < m.getsize(); i++)
{
m[i].show(); // 使用const T&
cout << endl;
}
}
void test()
{
base b1("zhangshan", 100);
myarr<base>a(5);
for(int i=0;i<5;i++)
a.push_back(b1);
myprintf(a);
}
int main()
{
test();
return 0;
}
```
bool Accum::operator==(const Accum& s) const { return(this->set == s.set && this->view == s.view && this->prepv == s.prepv && this->preph == s.preph && this->size == s.size && this->sign == s.sign); }
这段代码定义了 `Accum` 类的 `operator==` 运算符重载函数,用于比较两个 `Accum` 对象是否相等。
该函数使用逻辑与运算符 `&&` 来逐个比较两个对象的成员变量是否相等。如果所有的成员变量都相等,则返回 `true`,否则返回 `false`。
具体比较的成员变量包括:
- `set`:与另一个对象的 `set` 成员变量比较
- `view`:与另一个对象的 `view` 成员变量比较
- `prepv`:与另一个对象的 `prepv` 成员变量比较
- `preph`:与另一个对象的 `preph` 成员变量比较
- `size`:与另一个对象的 `size` 成员变量比较
- `sign`:与另一个对象的 `sign` 成员变量比较
如果以上所有的成员变量都相等,则返回 `true`,表示两个对象相等;否则返回 `false`,表示两个对象不相等。
这个运算符重载函数使得可以通过使用 `==` 运算符来比较两个 `Accum` 对象是否相等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)