简体   繁体   English

我可以访问临时对象的成员吗?

[英]Can I access members of temporary objects?

Can I access the member of an object if I create a temporary object by using class-type-name(parameters).member and presume that the constructor is done? 如果我通过使用class-type-name(parameters).member创建一个临时对象并假定构造函数已完成,是否可以访问该对象的成员?

Consider the following example: 考虑以下示例:

struct A
{
  enum status 
    { ERROR = -1, SUCCESS } state;
  A (int a) 
    : state(a > 0 ? SUCCESS : ERROR)
  {
    // do some stuff here
    // may change state
  }
};

int main (void)
{
  // Is this guaranteed to work?
  A::status S(A(5).state);
}

Is the constructor of A required to be done as soon as I access state ? 访问state是否需要完成A的构造函数?

Yes, the standard requires the implementation to perform all computations and side effects of the constructor of A before accessing state . 是的,该标准要求实现在访问state之前执行A的构造函数的所有计算和副作用。


Reference: 参考:

The expression X(Y).Z is a Postfix expression as per 5.2.5/1 of C++11: 根据C ++ 11的5.2.5 / 1,表达式X(Y).Z是Postfix表达式:

A postfix expression followed by a dot . 后缀表达式后跟一个点。 or an arrow ->, optionally followed by the keyword template (14.2), and then followed by an id-expression, is a postfix expression. 或一个箭头->(可选),后跟关键字模板(14.2),然后是id表达式,是后缀表达式。

The expression X(Y) is evaluated as per the same paragraph: 表达式X(Y)按照相同的段落求值:

The postfix expression before the dot or arrow is evaluated. 计算点或箭头之前的后缀表达式。 64 64

64 : If the class member access expression is evaluated, the subexpression evaluation happens even if the result is unnecessary to determine the value of the entire postfix expression, for example if the id-expression denotes a static member. 64如果评估了类成员访问表达式,则即使不需要结果来确定整个后缀表达式的值(例如,如果id表达式表示静态成员),也将进行子表达式评估。

That's where 1.9/14 applies: 那是1.9 / 14适用的地方:

Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated. 在与要评估的下一个完整表达式关联的每个值计算和副作用之前,对与一个完整表达式关联的每个值计算和副作用进行排序。

Therefore, the computations and side effects of X(Y) are done as soon as the dot operator is evaluated. 因此,一旦计算了点算子,就完成了X(Y)的计算和副作用。

This expression however generates a fully constructed object X as per 5.2.3/1: 但是,此表达式根据5.2.3 / 1生成完全构造的对象X

[...] the expression T(x1, x2, ...) is equivalent in effect to the declaration T t(x1, x2, ...); [...]表达式T(x1, x2, ...)实际上等效于声明T t(x1, x2, ...); for some invented temporary variable t , with the result being the value of t as a prvalue. 对于某些发明的临时变量t ,结果是t的值作为prvalue。

and 12.2/3: 和12.2 / 3:

When an implementation introduces a temporary object of a class that has a non-trivial constructor (12.1, 12.8), it shall ensure that a constructor is called for the temporary object. 当实现引入具有非平凡构造函数的类的临时对象(12.1、12.8)时,应确保为该临时对象调用构造函数。 [...] Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. [...]临时对象被销毁,是评估(按词法)包含创建点的完整表达式(1.9)的最后一步。

The standard requires the program to behave that way even if the acutal creation of the temporary object has not been performed (12.2/1): 该标准要求程序即使没有临时创建临时对象也要以这种方式进行操作(12.2 / 1):

Even when the creation of the temporary object is unevaluated (Clause 5) or otherwise avoided (12.8), all the semantic restrictions shall be respected as if the temporary object had been created and later destroyed. 即使对临时对象的创建未进行评估(第5节)或以其他方式避免(12.8),也应遵守所有语义限制,就好像临时对象已创建并随后被销毁一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问向量中存储的对象的成员? - How can I access the members of objects stored in a vector? 我可以禁止临时对象作为参数吗? - Can I forbid temporary objects as parameters? 如何检测对临时对象成员的引用 - how to detect references to members of temporary objects 我有一个基础对象和派生类对象的向量,但是我无法访问由存储在向量中的派生对象继承的数据成员 - I have a vector of Base and Derived Class Objects, but I can't access data members inherited by derived objects that are stored in the vector 如何在循环中访问结构的成员? - How can I access the members of a struct in a loop? 为什么我可以在临时std :: ofstream对象上使用`operator &lt;&lt;`? - Why can I use `operator<<` on temporary std::ofstream objects? 我可以在 C++ 中移动临时对象的属性吗? - Can I move attributes of temporary objects in C++? 为什么我不能直接在临时对象上调用operator()? - Why can't I call operator() on temporary objects directly? 我可以在同一个类的对象之间封装成员吗? - Can I encapsulate members between objects of the same class? 为什么仍然可以访问对临时对象的引用? - Why can I still access a reference to a temporary object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM