为什么不允许对一个临时对象进行非const引用,哪个函数getx()返回?显然,这是C++标准禁止的,但我对这种限制的目的感兴趣,而不是对标准的引用.
struct X
{
X& ref() { return *this; }
};
X getx() { return X();}
void g(X & x) {}
int f()
{
const X& x = getx(); // OK
X& x = getx(); // error
X& x = getx().ref(); // OK
g(getx()); //error
g(getx().ref()); //OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ref()可以修改临时对象. ref()允许您欺骗编译器并获取此临时对象的链接,这解决了我们的问题.此外:
他们说"为const引用分配一个临时对象可以延长这个对象的生命周期","但是对于非const引用却没有任何说法".我的其他问题.以下赋值是否延长了临时对象的生命周期?
X& x = getx().ref(); // OK
Run Code Online (Sandbox Code Playgroud) C++ 03 5.1主要表达式
§2:
文字是主要表达方式.它的类型取决于它的形式(2.13).字符串文字是左值; 所有其他文字都是右值.
这背后的理由是什么?
据我所知,字符串文字是对象,而所有其他文字都不是.并且l值总是指对象.
但问题是为什么字符串文字是对象,而所有其他文字都不是?
这个理由在我看来更像是鸡蛋或鸡肉问题.
我理解这个问题的答案可能与硬件架构有关,而不是C/C++作为编程语言,但我想听到同样的看法.
注意:我将此问题标记为c&c ++,因为C99标准也有类似的引用,特别是§6.5.1.4
在下面的代码中,我无法将临时对象作为参数传递给printAge函数:
struct Person {
int age;
Person(int _age): age(_age) {}
};
void printAge(Person &person) {
cout << "Age: " << person.age << endl;
}
int main () {
Person p(50);
printAge(Person(50)); // fails!
printAge(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
error: invalid initialization of non-const reference of type ‘Person&’ from an rvalue of type ‘Person’
Run Code Online (Sandbox Code Playgroud)
我意识到这与将lValue传递给期望rValue的函数有关...有没有办法通过使用std :: move或其他东西将我的lValue转换为rValue?我尝试了一个常量参数,但这似乎不起作用.
这听起来像是一个愚蠢的问题,但是我对以下行为感到困惑:
void funcTakingRef(unsigned int& arg) { std::cout << arg; }
void funcTakingByValue(unsigned int arg) { std::cout << arg; }
int main()
{
int a = 7;
funcTakingByValue(a); // Works
funcTakingRef(a); // A reference of type "unsigned int &" (not const-qualified)
// cannot be initialized with a value of type "int"
}
Run Code Online (Sandbox Code Playgroud)
经过仔细考虑,这是有道理的,因为在传递值时会创建一个新变量并可以进行转换,但是在传递变量的实际地址时并不需要那么多,就像在C ++中,一旦使变量的类型不能真的改变了。我认为这类似于这种情况:
int a;
unsigned int* ptr = &a; // A value of type int* cannot be used to
// initialise an entity of type "unsigned int*"
Run Code Online (Sandbox Code Playgroud)
但是,如果我使ref函数采用const,则转换有效:
void …Run Code Online (Sandbox Code Playgroud) 我尝试重载operator<<,但有警告我无法重载。我可以按如下方式重载该运算符:
std::cout << test4 << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是我不能按如下方式重载它:
std::cout << test2 + test3 << std::endl;
Run Code Online (Sandbox Code Playgroud)
我的主要代码是:
Stonewt test2(2, 8, Stonewt::STONE);
std::cout << "test2: " << test2 << std::endl;
Stonewt test3(2, 3, Stonewt::POUNDS);
std::cout << "test3: " << test3 << std::endl;
Stonewt test4 = test2 + test3;
std::cout << test4 << std::endl; // operator << can overload
std::cout << test2 + test3 << std::endl; // operator << cannot overload
Run Code Online (Sandbox Code Playgroud)
下面是friend功能
std::ostream& operator <<(std::ostream& os, Stonewt& a)
{
if …Run Code Online (Sandbox Code Playgroud) 我在C++中创建了一个带有重载运算符的矩阵类,我重载了运算符,<<(输出,使用ostream库),运算符+添加到矩阵,运算符=用于将一个矩阵分配给另一个矩阵.问题是,当我使用时
cout<<m1+m2<<endl;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
E0349: No operator << matches these operands type are: std::ostream << Matrix
Run Code Online (Sandbox Code Playgroud)
但如果我做以下事情:
Matrix m = m1 + m2;
cout<<m<<endl;
Run Code Online (Sandbox Code Playgroud)
它完美无缺.这是<<运算符:
ostream& operator<< (ostream &os,const Matrix& m)
{
if (m.isValid())
{
os << '|';
for (int i = 0; i < m.getRows(); i++)
{
for (int j = 0; j < m.getCols(); j++)
{
os << m.getMatrix()[i][j];
if (j < m.getCols() - 1)
{
os << ',';
}
}
os << '|';
}
}
else
{
os …Run Code Online (Sandbox Code Playgroud)