左值左值 [英] lvalue rvalue

查看:88
本文介绍了左值左值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下编译:

// syrup.cpp


struct DoubleInDisguise

{

双倍数据;

};


double Chocolate1()

{

double blah = 67.22;


返回等等;

}


DoubleInDisguise Chocolate2()

{

DoubleInDisguise blah = {67.22};


返回blah;

}

/ *

inline void Manipulate(double& input)

{

input = 222.76;

}

* /


inline void Manipulate(DoubleInDisguise& input)

{

//input.data = 222.76 ;

}


int main()

{

// Manipulate(Chocolate1() );


Chocolate2()= DoubleInDisguise();


//操纵(Chocolate2());

}

看看Chocolate2()的返回值如何对其进行分配。

这表明首先是它的非常数,其次是它是一个价值
左值。但是现在,看看我的最后一行代码,带走//评论者。它不会编译
。你可以指定一个临时的,但它不能作为一个双b& ;?!到底是怎么回事?


-JKop

解决方案

JKop写道:

以下编译:

// syrup.cpp

struct DoubleInDisguise
{
双数据;
};

双巧克力1()
{
双重blah = 67.22;

返回blah;
}

DoubleInDisguise Chocolate2 ()
{
DoubleInDisguise blah = {67.22};

返回blah;
}

/ *
内联无效操作(双和输入)
{
输入= 222.76;
}
* /

内联void操作(DoubleInDisguise& input)
{
//input.data = 222.76;
}
int main()
//
// Manipulate(Chocolate1());

Chocolate2()= DoubleInDisguise();



以上行执行此操作:Chocolate2()返回一个临时的

DoubleInDisguise对象,然后将其赋值为

临时在右边(初始化为0)。

//操作(Chocolate2());




上面的内容是双倍作为参数,而你传递的是一个

DoubleInDisguise。



问候,


Ioannis Vranos

http:// www23 .brinkster.com / noicys


Ioannis Vranos写道:

JKop写道:

以下编译:

// syrup.cpp

struct DoubleInDisguise
{
双数据;
} ;

双巧克力1()
{
双重blah = 67.22;

返回blah;
}
DoubleInDisgui se Chocolate2()
{
DoubleInDisguise blah = {67.22};

返回blah;
}

/ *
inline void Manipulate(double&输入)
{
input = 222.76;
}
* /
内联void操作(DoubleInDisguise& input)
{
//input.data = 222.76;
}
int main()
//
// Manipulate(Chocolate1());

Chocolate2()= DoubleInDisguise();



上面这行是这样的:Chocolate2()返回一个临时的
DoubleInDisguise对象,然后赋予该值
临时在右边(初始化为0)。


// Manipulate(Chocolate2());




以上是传递给虚空操纵(DoubleInDisguise&)一个

临时类型DoubleInDisguise这是不允许的,因为它是

获得参考。你可以做到的唯一方法就是使功能


inline void Manipulate(const DoubleInDisguise& input)

{

//input.data = 222.76;

}
带有const引用的
。我建议你阅读并阅读TC ++ PL 3,你会在本书中找到很多知识。



问候,

Ioannis Vranos

http:/ /www.3.brinkster.com/noicys


JKop写道:


以下编译:

// syrup.cpp

结构DoubleInDisguise
{
双数据;
};

double Chocolate1()
{
双重blah = 67.22;

返回blah;
}

DoubleInDisguise Chocolate2()
{
DoubleInDisguise blah = {67.22};

返回blah;
}

/ *
inline void Manipulate(double& input)
{
input = 222.76;
}
* /

内联void操作(DoubleInDisguise& input)
{
//input.data = 222.76;
}
int main()
{
// Manipulate(Chocolate1());

Chocolate2()= DoubleInDisguise();

//操纵(Chocolate2());
}
了解Chocolate2()的返回值如何对其进行分配。
这表明它首先是非const,其次是它是
左值。




[...]


不,这是一个左值。是的,对于内置类型,您只能分配一个

左值。但由于DoubleInDisguise是一个用户定义的类型,它有一个成员

函数operator =(语义上,无论如何),可以在右值上调用。


为了防止这种混乱,你可以将Chocolate2更改为

DoubleInDisguise const Chocolate2(){...}


Denis


The following compiles:
// syrup.cpp

struct DoubleInDisguise
{
double data;
};

double Chocolate1()
{
double blah = 67.22;

return blah;
}

DoubleInDisguise Chocolate2()
{
DoubleInDisguise blah = { 67.22 };

return blah;
}
/*
inline void Manipulate(double& input)
{
input = 222.76;
}
*/

inline void Manipulate(DoubleInDisguise& input)
{
//input.data = 222.76;
}

int main()
{
//Manipulate( Chocolate1() );

Chocolate2() = DoubleInDisguise();

// Manipulate( Chocolate2() );
}
See how the return-value from Chocolate2() can have an assigment done to it.
This suggests that its non-const first of all, and secondly that it''s an
lvalue. But now, see my last line of code, take away the // commenters. It
won''t compile. You can assign to a temporary, yet it can''t act as a
double&?! What the hell is going on?

-JKop

解决方案

JKop wrote:

The following compiles:
// syrup.cpp

struct DoubleInDisguise
{
double data;
};

double Chocolate1()
{
double blah = 67.22;

return blah;
}

DoubleInDisguise Chocolate2()
{
DoubleInDisguise blah = { 67.22 };

return blah;
}
/*
inline void Manipulate(double& input)
{
input = 222.76;
}
*/

inline void Manipulate(DoubleInDisguise& input)
{
//input.data = 222.76;
}

int main()
{
//Manipulate( Chocolate1() );

Chocolate2() = DoubleInDisguise();

The above line does this: Chocolate2() returns a temporary
DoubleInDisguise object which is then assigned the value of the
temporary on the right (which is initialised to 0).


// Manipulate( Chocolate2() );



The above takes a double as an argument while you are passing it a
DoubleInDisguise.


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys


Ioannis Vranos wrote:

JKop wrote:

The following compiles:
// syrup.cpp

struct DoubleInDisguise
{
double data;
};

double Chocolate1()
{
double blah = 67.22;

return blah;
}

DoubleInDisguise Chocolate2()
{
DoubleInDisguise blah = { 67.22 };

return blah;
}
/*
inline void Manipulate(double& input)
{
input = 222.76;
}
*/

inline void Manipulate(DoubleInDisguise& input)
{
//input.data = 222.76;
}

int main()
{
//Manipulate( Chocolate1() );

Chocolate2() = DoubleInDisguise();



The above line does this: Chocolate2() returns a temporary
DoubleInDisguise object which is then assigned the value of the
temporary on the right (which is initialised to 0).


// Manipulate( Chocolate2() );



The above is passing to the void Manipulate(DoubleInDisguise&) a
temporary of type DoubleInDisguise which is not allowed since it is
getting a reference. The only way you can do it is by making the function

inline void Manipulate(const DoubleInDisguise& input)
{
//input.data = 222.76;
}
that is with a const reference. I suggest you get and read TC++PL 3, you
will find much knowledge in this book.


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys


JKop wrote:


The following compiles:

// syrup.cpp

struct DoubleInDisguise
{
double data;
};

double Chocolate1()
{
double blah = 67.22;

return blah;
}

DoubleInDisguise Chocolate2()
{
DoubleInDisguise blah = { 67.22 };

return blah;
}

/*
inline void Manipulate(double& input)
{
input = 222.76;
}
*/

inline void Manipulate(DoubleInDisguise& input)
{
//input.data = 222.76;
}

int main()
{
//Manipulate( Chocolate1() );

Chocolate2() = DoubleInDisguise();

// Manipulate( Chocolate2() );
}

See how the return-value from Chocolate2() can have an assigment done to it.
This suggests that its non-const first of all, and secondly that it''s an
lvalue.



[...]

No, it is an rvalue. Yes, for built-in types, you can only assign to an
lvalue. But since DoubleInDisguise is a user-defined type, it has a member
function operator = (semantically, anyway), which can be called on an rvalue.

To prevent this kind of confusion, you can change Chocolate2 to
DoubleInDisguise const Chocolate2() {...}

Denis


这篇关于左值左值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆