为什么C++;函数对象需要引用类型成员变量吗? 这是一个新手C++问题。我在读维基百科上的“函数对象”一文。本文中有一个C++的例子,类似于: struct printClass { int &count; printClass(int &n) : count(n) {} void operator()(int &i) const { count++; cout << i << "[" << count << "] "; } }; int main(int argc, char** argv) { vector<int> a(5, 7); a[4] = -1; a.resize(10, 3); int state = 0; for_each(a.rbegin(), a.rend(), printClass(state)); } struct printClass { int count; printClass(int n) { count = n; } void operator()(int &i) { count++; cout << i << "[" << count << "] "; } };
编辑:根据iammilind的回复,这是第三个版本,它也可以使用为什么C++;函数对象需要引用类型成员变量吗? 这是一个新手C++问题。我在读维基百科上的“函数对象”一文。本文中有一个C++的例子,类似于: struct printClass { int &count; printClass(int &n) : count(n) {} void operator()(int &i) const { count++; cout << i << "[" << count << "] "; } }; int main(int argc, char** argv) { vector<int> a(5, 7); a[4] = -1; a.resize(10, 3); int state = 0; for_each(a.rbegin(), a.rend(), printClass(state)); } struct printClass { int count; printClass(int n) { count = n; } void operator()(int &i) { count++; cout << i << "[" << count << "] "; } };,c++,function-object,C++,Function Object,编辑:根据iammilind的回复,这是第三个版本,它也可以使用const\u cast struct printClass { int count; printClass(int n) { count = n; } void operator()(int &i) { count++; cout << i << "[" << count << "] "; } }; struct printCl
const\u cast
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
struct printClass{
整数计数;
printClass(int n):计数(n){
void运算符()(int&i)常量{
常量(计数)+;
库特
(1) 当count是一个正则变量而不是一个
参考类型
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
这是一个非常有趣的问题。问题应该是,当count
声明为引用时,为什么代码会编译:
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
正则变量失败,因为int count
在const
限定函数operator()(int&i)const;
中不可修改
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
引用差别不大。在代码中,您将方法声明为const
,这意味着引用i
的count
现在不能引用任何其他内容。
但由于引用的性质,这无论如何是不可能的:)。初始化后无法更改引用绑定。
operator()
只是检查您是否正在将count
的绑定更改为其他内容?答案总是否定的。因为count++
更改了count
引用的值,而不是绑定
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
在代码中,当涉及到int&count
时,成员方法是否为const
并不重要
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
将int&count;
与int*const p\u count;
关联,并尝试自己模拟情况
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
(2) 为什么编译失败?我将ctor更改为以下内容?
CountFrom(int&n){count=n;}
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
因为初始化时必须将引用分配给变量
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
int i, &r; // error, because 'r' not initialized
r = i; // this is not initialization but a copy
另一方面,在处理类
中的引用变量时应格外小心,因为很容易弄乱它们的范围和有效性。
例如,count
的有效性取决于i
的范围
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
编辑:在第二次编辑之后,很难知道该版本的工作原理。
因为count
现在是一个简单的变量。与引用不同,它可以从构造函数的初始值设定项列表中的初始化中跳过。
此外,操作符()
的const
正确性已经消失,因此类
的任何成员变量现在都可以在其内部修改
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};
如果要声明类
成员“必须”在其内部更改,则应选择成员方法的常量
版本(除非任何变量是可变的
或使用常量转换
)。在所有其他情况下,使用普通版本的成员方法。它们也可以共存,具体取决于您的业务逻辑。这是一个比较宽泛的问题,值得使用另一个线程来解决此问题。请向我们展示您试图编译的确切代码,以及确切的错误消息你明白了。我已经用我正在尝试的确切例子更新了这个问题。PTAL
struct printClass {
int count;
printClass(int n) { count = n; }
void operator()(int &i) {
count++;
cout << i << "[" << count << "] ";
}
};