简体   繁体   English

对象指针打印结果顺序相反

[英]Object pointers printing results in reverse order

I was trying to use a pointer to iterate through an object array. 我试图使用指针迭代对象数组。 Printing values one at a time seems to work fine but it's giving the result in a reversed order when I try to print them all together. 一次打印一个值似乎工作正常,但当我尝试将它们全部打印在一起时,它会以相反的顺序给出结果。

#include<iostream>
using namespace std;

class car{
    int tires;
public:
    car(int a) {tires = a;};
    int GetTires(){return tires;};
};

int main() {
    car array[4] = { 2, 4, 6, 8 };
    car *p;

    p = array;
    cout << ((p++)->GetTires()) << " " << ((p++)->GetTires()) << " " << ((p++)->GetTires())  << " "  << ((p++)->GetTires()) << endl;
   //Prints: 8 6 4 2 

    p = array;
    cout << ((p++)->GetTires()) << " ";
    cout << ((p++)->GetTires()) << " ";
    cout << ((p++)->GetTires()) << " ";
    cout << ((p++)->GetTires()) << " " << endl;
    //Prints: 2 4 6 8

    return 0;
}

Using the C function 'printf' also gives the same output. 使用C函数'printf'也可以提供相同的输出。 Can anyone explain why this is happening? 任何人都可以解释为什么会这样吗? TIA. TIA。

If you use an older than C++17 compiler, then the order of (p++)->GetTires() evaluations in std::cout::operator << is undefined. 如果使用的是早于C ++ 17的编译器,那么std::cout::operator <<(p++)->GetTires()求值的顺序是未定义的。 Thus your code produces undefined behaviour. 因此您的代码会产生未定义的行为。 Another compiler or your compiler with other compiler settings may produce another output due to another evaluation order. 由于另一个评估顺序,另一个编译器或您的编译器与其他编译器设置可能会产生另一个

Between the previous and next sequence point, the prior value of a scalar object that is modified by the evaluation of the expression, must be accessed only to determine the value to be stored. 在前一个和下一个序列点之间,必须访问由表达式求值修改的标量对象的先前值,以确定要存储的值。 If it is accessed in any other way, the behavior is undefined. 如果以任何其他方式访问它,则行为未定义。

 cout << i << i++; // undefined behavior (until C++17) a[i] = i++; // undefined behavior (until C++17) 

Recommend to read: What are the evaluation order guarantees introduced by C++17? 建议阅读: C ++ 17引入的评估顺序保证是什么?

Code

cout << ((p++)->GetTires()) << " " << ((p++)->GetTires()) << " " << ((p++)->GetTires())  << " "  << ((p++)->GetTires()) << endl;

does not do same as 做不一样的

    cout << ((p++)->GetTires()) << " ";
    cout << ((p++)->GetTires()) << " ";
    cout << ((p++)->GetTires()) << " ";
    cout << ((p++)->GetTires()) << " " << endl;

This is how typical operator<< looks 这就是典型的运算符<<看起来如何

ostream& operator<<(ostream& os, const Type& dt)  
{  
    os << dt.value;  
    return os;  
} 

What happens in first line is that compiler would evaluate all sub-expressions that contain increment operator. 第一行中发生的是编译器将评估包含增量运算符的所有子表达式。 Order of evaluation isn't defined by standard but and might be not in direction of execution. 评估顺序不是由标准定义的,但可能不是执行方向。 In result last sub-expression is evaluated first and increment carried over to 3rd third expression and so on. 在结果中,首先评估最后一个子表达式,并将增量转移到第三个表达式,依此类推。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM