首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

300分一直以来尚未解决的有关问题

2012-03-15 
300分求一个一直以来尚未解决的问题C/C++坛子一直以来很少有提问者给300分,我就尝试发一个吧,免得其它版说

300分求一个一直以来尚未解决的问题
C/C++坛子一直以来很少有提问者给300分,我就尝试发一个吧,免得其它版说咱C/C++太寒酸了。不过,不要把300分与下面的问题联系起来,这问题可能连30分也不值,但的确是我尚未彻底弄清楚的疑问。

虽然这贴子发在C++版,但不局限于C++,可以放在C90、C99、C++98、C++2003和C++11等角度下讨论。下面代码在C中的行为很容易弄清楚,因此我着重关心的是C++中的表现。

C/C++ code
struct A{    static int k;    int i;};........int A::k = 10;........A fun( ){ A a; return a; }........fun( ).k = 20;fun( ).i = 30;


fun( ).k是左值,这个已被证明,那么,fun( ).i是左值吗?(提示:不要被赋值运算符的左操作数迷惑了)。

[解决办法]
我个人觉得不是,估计下面的标准描述您也看过。

3.10 Lvalues and rvalues [basic.lval]

2 An lvalue refers to an object or function. Some rvalue expressions—those of class or cv-qualified class type—also refer to objects.47)

47) Expressions such as invocations of constructors and of functions that return a class type refer to objects, and the implementation can invoke a member function upon such objects, but the expressions are not lvalues.
[解决办法]
贴出我的理由:
Some rvalue expressions—those of class or cv-qualified class type—also refer to objects.
关于rvalue expressions 标准有一些附注解释:
Expressions such as invocations of constructors and of functions that return a class type refer to objects, and the implementation can invoke a member function upon such objects, but the expressions are not lvalues.

再配合:
An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [Example: a member function called for an object (9.3) can modify the object. ]

虽然上面是说通过成员函数的调用来修改。
[解决办法]
fun().i 是右值。
参见 n3290 3.10/1 bullet 4,这一其中红色部分。
An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object.

下面是一个例子程序证明。
C/C++ code
#include <iostream>struct A{ static int k; int i;};int A::k = 10;A fun( ){ A a; return a; }void f (int& ) { std::cout << "int& " << std::endl; }void f (int&&) { std::cout << "int&&" << std::endl; }int main (){ fun().k = 20; f(fun().i); return 0;}
[解决办法]
探讨
...........
我的VC 10.0怎么是int&呢?

热点排行