创建 C++ 整数类以与整数类型完全相同 [英] Create C++ integer class to act absolutely identical to integral integer type

查看:68
本文介绍了创建 C++ 整数类以与整数类型完全相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天前看到的一个小问题,很讨厌,面试时问了我的朋友.

Small and pretty nasty problem I've seen several days ago, asked to my friend on interview.

最初的面试问题是:以下代码的输出是什么?"

The initial interview question was: "What will be the output of the following code?"

int i = 2;
i = i++ + i++;

正确答案是 ((2 + 2) + 1) + 1 = 6,即在赋值之前应用两次后增量,但在添加之后.

The correct answer is ((2 + 2) + 1) + 1 = 6, i.e. post-increment is applied twice before assignment, but after addition.

然后我想创建一个简单的类,其中包含一个整数和重载 operator+() 和 operator++(int) 以在日志中查看操作符执行的确切顺序.

Then I wanted to create a simple class carrying one integer and overload operator+() and operator++(int) to see in logs the exact order, in which operators will be executed.

这是我得到的:

class A
{
public:
A(int _data) : data(_data) { }

A &operator=(const A& _rhs)
{
    data = _rhs.data;
    cout<<" -- assign: "<<data<<endl;
}

A operator++(int _unused)
{
    A _tmp = data;
    data++;

    cout<<" -- post-increment: "<<data<<endl;
    return _tmp;
}

A operator+(const A &_rhs)
{
    A _tmp = data + _rhs.data;

    cout<<" -- addition: "<<data<<"+"<<_rhs.data<<endl;
    return _tmp;
}

inline operator int() const { return data; }

private:
    int data;
};

结果非常令人沮丧:

-- post-increment: 3
-- post-increment: 4
-- addition: 3+2
-- assign: 5

对于不太复杂的结构,例如 (A _dt2 = a++; ),它按其应有的方式运行,但运算符的执行顺序与整数类型不同.

For less sophisticated constructions, such as (A _dt2 = a++; ), it acts as it should, but the order of operators execution is not as for integral types.

我猜这可能是编译器特定的问题:

It might be compiler specific problem, I guess:

$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

所以,我有点失落:)

推荐答案

最初的面试问题是:以下代码的输出是什么?"

The initial interview question was: "What will be the output of the following code?"

int i = 2;
i = i++ + i++;

正确答案是未定义行为,因为您要多次修改同一个变量,中间没有序列点.

The correct answer is undefined behaviour, because you're modifying the same variable multiple times without a sequence point in between.

C++03 标准 §5 [expr] p4:

除非另有说明,未指定单个运算符的操作数和单个表达式的子表达式的计算顺序以及副作用发生的顺序.

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.

这可能无法回答您真正的问题,但即使您创建一个类似整数的类并重载 operator++(int)operator+(A const&).函数参数求值的顺序是不确定的,可以按照编译器喜欢的任何顺序进行,因此结果是不确定的.

This may not answer your real question, but it will be similar even if you make an integer-like class and overload the operator++(int) and operator+(A const&). The order of evaluation of arguments to a function is unspecified, it may be done in any order the compiler likes, thus the result is unspecified.

这篇关于创建 C++ 整数类以与整数类型完全相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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