相关疑难解决方法(0)

文字字符串和函数返回值是左值还是右值?

  1. 只是想知道文字字符串是左值还是左值.其他文字(如int,float,char等)是左值还是右值?

  2. 函数的返回值是左值还是右值?

你怎么说出差异?

c rvalue lvalue

30
推荐指数
1
解决办法
1万
查看次数

需要左值

"Lvalue required"的错误信息实际上是什么意思?

c lvalue

10
推荐指数
2
解决办法
3万
查看次数

隐式转换:const引用vs非const引用vs非引用

考虑一下这段代码

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隐式地转换为Bconst B&,但不会转换为B&导致编译错误.

c++ reference const-reference implicit-conversion

8
推荐指数
1
解决办法
4392
查看次数

三元运营商评估订单

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.为什么?

c++ ternary-operator ternary

2
推荐指数
2
解决办法
3227
查看次数

C++中奇怪的赋值/乘法运算符行为

有人可以解释我运营商的错误:

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)

c++ multiplication variable-assignment operator-keyword

2
推荐指数
2
解决办法
303
查看次数

检索有关&& x的信息,该信息保留&x的地址

我想用以下代码探索指针的专长:

#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)

c

1
推荐指数
1
解决办法
42
查看次数