写入ostream的常量引用 我正在阅读C++底漆。我遇到了以下代码: #include <iostream> #include <string> using namespace std; class PrintString { public: PrintString(ostream &o = cout, char c = ' '): os(o), sep(c) {} void operator() (const string &s) const { os << s << sep; } private: ostream &os; char sep; }; int main() { const PrintString printer; printer("ABC"); return 0; } #包括 #包括 使用名称空间std; 类打印字符串{ 公众: PrintString(ostream&o=cout,char c=''):os(o),sep(c){} void operator()(const string&s)const{os
引用没有被修改,只修改它所引用的内容。这与指针的作用相同。如果您有指向写入ostream的常量引用 我正在阅读C++底漆。我遇到了以下代码: #include <iostream> #include <string> using namespace std; class PrintString { public: PrintString(ostream &o = cout, char c = ' '): os(o), sep(c) {} void operator() (const string &s) const { os << s << sep; } private: ostream &os; char sep; }; int main() { const PrintString printer; printer("ABC"); return 0; } #包括 #包括 使用名称空间std; 类打印字符串{ 公众: PrintString(ostream&o=cout,char c=''):os(o),sep(c){} void operator()(const string&s)const{os,c++,reference,constants,cout,C++,Reference,Constants,Cout,引用没有被修改,只修改它所引用的内容。这与指针的作用相同。如果您有指向int数据成员(int*)的指针,在const成员函数中使用它将使其类型int*const。您无法更改指针本身,但可以更改它指向的内容。例如: struct Foo { int a; int* p = &a; void foo() const { p = new int; // ERROR! Not allowed to modify const pointer
int
数据成员(int*
)的指针,在const
成员函数中使用它将使其类型int*const
。您无法更改指针本身,但可以更改它指向的内容。例如:
struct Foo
{
int a;
int* p = &a;
void foo() const
{
p = new int; // ERROR! Not allowed to modify const pointer
*p = 100; // OK, it's a pointer to a non-const int
}
};
因此,当使用
os
时,您只修改它引用的对象,而不是引用本身。尝试将引用成员视为常量指针成员变量,以及当您声明此类的常量对象时,常量将如何传播到成员变量。得到常量的是指针本身,而不是指针指向的对象同样的语义也适用于引用,问题是引用已经是const(像一个const指针变量),所以对它们没有任何改变
#include <iostream>
struct S {
int *p;
int *const cp;
int &ref;
};
int main() {
using namespace std;
int i = 10;
const S s{&i, &i, i};
// s.p = &i; // can't do this, s.p gets const
*s.p = 20; // changing the pointee
cout << i << endl;
// s.p = &i; // can't do this, s.p was const already, and would get if it weren't
*s.cp = 30; // changing the pointee
cout << i << endl;
// s.ref ?; // can't make a reference refer to other object
s.ref = 40; // changing the pointee
cout << i << endl;
}
#包括
结构{
int*p;
int*常数cp;
int&ref;
};
int main(){
使用名称空间std;
int i=10;
常数S{&i,&i,i};
//s.p=&i;//无法执行此操作,s.p获取常量
*s、 p=20;//更改指针对象
cout你对常量的混淆最好用指针而不是引用来解释
假设你有:
struct A {int data;};
struct B
{
B(A* ptr) : aPtr(ptr) {}
A* aPtr;
};
int main()
{
A a1;
A a2;
const B b(&a1);
// This makes b a const object.
// This makes b.aPtr a const pointer. That means, you cannot change where it points to
// but you can still change the value of what it points to.
b.aPtr = &a2; // Not ok.
b.aPtr->data = 10; // OK.
}
b.aPtr
的常量与使用原始指针时的常量类似
int main()
{
A a1;
A a2;
A* const aPtr1 = &a1;
// This makes aPtr1 a const pointer. That means, you cannot change where it points to
// but you can still change the value of what it points to.
aPtr1 = &a2; // Not ok.
aPtr1->data = 10; // OK.
A const* aPtr2 = &a1;
// This makes aPtr2 a pointer to a const object. That means, you can change where it points to
// but you cannot change the value of what it points to.
aPtr2 = &a2; // ok.
aPtr2->data = 10; // Not ok.
}
当涉及到引用时,它是相似的,但有一点扭曲。没有非const
引用这样的东西。一个引用一旦初始化,就不能引用另一个对象
A a1;
A& ref = a1; // ref cannot be changed to reference any other object.
就你而言
const PrintString printer;
对成员变量PrintString::os
的const
-ness没有影响。它继续引用非const
ostream
。这允许您使用:
const PrintString printer;
printer("ABC");