我有以下示例代码:
UInt16 a = 0x3A;
UInt16 b = 0xFFDF;
UInt16 result = Convert.ToUInt16(a - b);
Run Code Online (Sandbox Code Playgroud)
带有溢出异常的第3行错误.但是我希望得到相同的结果,就像我在C中减去2个无符号短路并且它们上/下溢.
实现这一目标的最恰当方法是什么?
我无法弄清楚为什么(似乎)一个物体被毁坏了两次.
如果我创建一个类(B)的对象,其中包含另一个类(A)的对象,我复制此对象.复制的对象被破坏两次.它看起来像这样.我无法弄清楚这个输出.
我创建了以下(最小?)示例,它似乎触发了我的问题:
#include <stdio.h>
#include <stdint.h>
template <class T>
class A
{
public:
A()
{
myCtr = ++ctr;
printf("class A default Constructor - object id: %u\n", myCtr);
}
A(const A<T> &a2) {
myCtr = ++ctr;
printf("class A copy constructor - object id: %u\n", myCtr);
}
~A()
{
printf("class A destructor - object id: %u\n", myCtr);
}
void add(T item) {
/* Irrelevant */
}
private:
uint64_t myCtr;
static uint64_t ctr;
};
class B
{
public:
B() {
}
B(char …Run Code Online (Sandbox Code Playgroud)