只是想知道文字字符串是左值还是左值.其他文字(如int,float,char等)是左值还是右值?
函数的返回值是左值还是右值?
你怎么说出差异?
考虑一下这段代码
struct A {};
struct B { B(const A&) {} };
void f(B)
{
cout << "f()"<<endl;
}
void g(A &a)
{
cout << "g()" <<endl;
f(a); //a is implicitly converted into B.
}
int main()
{
A a;
g(a);
}
Run Code Online (Sandbox Code Playgroud)
这编译正常,运行良好.但如果我f(B)改为f(B&),它就不会编译.如果我写f(const B&),它再次编译好,运行正常.原因和理由是什么?
摘要:
void f(B); //okay
void f(B&); //error
void f(const B&); //okay
Run Code Online (Sandbox Code Playgroud)
我希望听到语言规范中的原因,理由和参考,以及每种情况.当然,功能签名本身并不正确.而是A隐式地转换为B和const B&,但不会转换为B&导致编译错误.
class Foo {
public:
explicit Foo(double item) : x(item) {}
operator double() {return x*2.0;}
private:
double x;
}
double TernaryTest(Foo& item) {
return some_condition ? item : 0;
}
Foo abc(3.05);
double test = TernaryTest(abc);
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,如果some_condition为真,为什么test等于6(而不是6.1)?
如下更改代码会返回6.1的值
double TernaryTest(Foo& item) {
return some_condition ? item : 0.0; // note the change from 0 to 0.0
}
Run Code Online (Sandbox Code Playgroud)
似乎(在原始示例中)来自Foo :: operator double的返回值被强制转换为int,然后返回到double.为什么?
有人可以解释我运营商的错误:
Matrix3D Matrix3D::operator*(Matrix3D& m) {
Matrix3D ret;
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
ret._data[i][j]=0.0;
for(int k=0;k<4;k++) {
ret._data[i][j] += (this->_data[i][k]*m._data[k][j]);
}
}
}
return ret;
}
Matrix3D& Matrix3D::operator=(Matrix3D& m) {
if(this==&m) {
return *this;
}
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
this->_data[i][j] = m._data[i][j];
}
}
return *this;
}
Matrix3D Matrix3D::Rotation(double ax, double ay, double az) {
Matrix3D rotX;
Matrix3D rotY;
Matrix3D rotZ;
rotX(
1, 0, 0, 0,
0, cos(ax), -sin(ax), 0,
0, sin(ax), cos(ax), 0,
0, 0, 0, 1 …Run Code Online (Sandbox Code Playgroud) 我想用以下代码探索指针的专长:
#include <stdio.h>
int x = 3;
int main(void)
{
printf("x's value is %d, x's address is %p", x, &x);
//printf("x's address is stored in", &&x);
}
Run Code Online (Sandbox Code Playgroud)
它工作正常并获得输出
$ ./a.out
x's value is 3, x's address is 0x10b1a6018
Run Code Online (Sandbox Code Playgroud)
当我利用时&x,为它保留一个存储空间以保持地址0x10b1a6018,因此打印一个地址.
接下来,我打算获取有关存储另一个地址的地址的信息.
#include <stdio.h>
int x = 3;
int main(void)
{
printf("x's value is %d, x's address is %p", x, &x);
printf("x's address is stored in", &&x);
}
Run Code Online (Sandbox Code Playgroud)
但它报告错误为:
$ cc first_c_program.c
first_c_program.c:14:40: warning: data argument not used by format …Run Code Online (Sandbox Code Playgroud)