我运行此代码来试验复制构造函数和赋值运算符
class AClass {
private:
int a;
public:
AClass (int a_) : a(a_) {
cout << " constructor AClass(int) " << a << endl;
}
AClass(const AClass & x) : a(x.a) {
cout << " copy constructor AClass(const AClass &) " << a << endl;
}
AClass & operator=(const AClass & x) {
a = x.a;
cout << " AClass& operator=(const AClass &) " << a - endl;
return *this;
}
};
AClass g () {
AClass x(8);
return x; …Run Code Online (Sandbox Code Playgroud) 我正在学习c.我有个问题.为什么我的程序不起作用?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char cmd;
void exec()
{
if (cmd == "e")
{
printf("%c", cmd);
// exit(0);
}
else
{
printf("Illegal Arg");
}
}
void input()
{
scanf("%c", &cmd);
exec();
}
int main()
{
input();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我插入一个"e",但它说非法arg.
cmd不等于"e".为什么?我用scanf将cmd设置为"e".
我的程序使用一个简单的结构Rect,定义为
struct Rect {
int x1, y1, x2, y2;
Rect()
: x1(0), y1(0), x2(0), y2(0) { }
Rect(int x1, int y1, int x2, int y2)
: x1(x1), y1(y1), x2(x2), y2(y2) { }
};
Run Code Online (Sandbox Code Playgroud)
我应该定义一个复制/移动构造函数或赋值运算符,还是可以依赖编译器来自动生成它们?问题在于速度和使用原因(例如,移动构造函数可以影响程序执行速度).
构造函数和运算符是非常重复的工作,所以如果我可以依赖编译器自动生成它们会很好.
Rect(const Rect& r)
: x1(r.x1), y1(r.y1), x2(r.x2), y2(r.y2) { }
Rect(Rect&& r)
: x1(r.x1), y1(r.y1), x2(r.x2), y2(r.y2) { }
Rect& operator = (const Rect& r) {
x1 = r.x1;
y1 = r.y1;
x2 = r.x2;
y2 = r.y2;
}
Run Code Online (Sandbox Code Playgroud) c++ copy-constructor assignment-operator move-semantics c++11
我正在研究一些Java,因为我遇到了下面这段代码.我理解典型的三元运算符(例如下面以"boolean a"开头的行),但我无法理解如何读取以"boolean b"开头的行上的表达式.任何有关如何阅读此行的帮助将不胜感激!谢谢!
public class Ternary{
public static void main (String[] args){
int x = 10;
int i = 2;
boolean a = x > 10 ? true: false;
boolean b = a = true ? ++i > 2 ? true:false:false;
System.out.print(b);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为几个类(C++)编写单元测试,并遇到了一个试图为复制构造函数和赋值运算符编写单元测试的问题.一个基本的问题是,程序员将一个成员添加到类中,然后忘记更新c'ctor和/或operator =.
我当然可以按照以下方式编写单元测试:
class MyClass()
{
public:
int a, b;
non_trivial_copyable nasty;
MyClass& operator=(const MyClass& _r)
{
if(this == &r)
return *this;
a = _r.a;
b = _r.b;
nasty = acquire_non_trivial_copyable();
}
};
TEST(My_Class_copy_op)
{
MyClass m1;
m1.a = m1.b = 2;
MyClass m2 = m1;
VERIFY(m2.a == 2);
VERIFY(m2.b == 2);
}
Run Code Online (Sandbox Code Playgroud)
很好.现在程序员添加了一个成员c,但是没有更新运算符和测试用例.
class MyClass()
{
public:
float c;
// ...
}
Run Code Online (Sandbox Code Playgroud)
即使操作员现在已经破损,测试用例仍将快乐地成功.
现在,我们可以做到以下几点:
TEST(My_Class_copy_op)
{
// Aha! Fails when programmer forgets to update test case to …Run Code Online (Sandbox Code Playgroud) 对于struct有const会员
struct point { const int x; const int y; };
Run Code Online (Sandbox Code Playgroud)
用作会员数据
struct Foo
{
point pt{ 0, 0 };
void move_x(int value);
};
Run Code Online (Sandbox Code Playgroud)
如何Foo::move_x()写更新Foo::pt?可以使用memcpy()吗?
#include <memory.h>
void Foo::move_x(int value)
{
const point pt_{ pt.x + value, pt.y };
(void) memcpy(&pt, &pt_, sizeof(pt_)); // pt = pt_;
}
Run Code Online (Sandbox Code Playgroud)
这可以使用指针安全地完成
#include <memory>
struct Bar
{
std::unique_ptr<point> pPt_{ new point{ 0, 0 } };
const point& pt() const {
return *pPt_;
} …Run Code Online (Sandbox Code Playgroud) 可能是一个骗局,但我找不到它.
在敲击键盘两天之后,我发现重载equals运算符(operator=)显然会中断std::sort.也许我的输operator=错不正确?这是我的MCVE:
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <cstdint>
#include <vector>
struct Person
{
std::string name;
uint32_t age;
bool operator< (const Person& p)
{
return this->age < p.age;
}
Person operator= (const Person& p)
{
Person newP;
newP.name = p.name;
newP.age = p.age;
return newP;
}
static bool SortPeople(const Person& p1, const Person& p2)
{
return p1.age < p2.age;
}
};
void PrintPeople(const std::vector<Person>& people)
{
std::cout << "============ people begin" …Run Code Online (Sandbox Code Playgroud) 所以正如标题所示,我有一个使用临时数组的函数,我想从另一个数组中写入一个值,然后将这两个值与它自身相乘.
例:
float[] a = {0, 0}
a[0] *= a[0] = b[n ];
a[1] *= a[1] = b[n + 1];
Run Code Online (Sandbox Code Playgroud)
我希望上面会做到以下几点:
a[0] = b[n ];
a[0] *= a[0]; //(aka: a[0] = a[0] * a[0])
a[1] = b[n + 1];
a[1] *= a[1];
Run Code Online (Sandbox Code Playgroud)
虽然这种行为似乎并没有发生.相反,它似乎只是乘以"a"中的原始值与"b"中保存的任何值相乘,如下所示:
a[0] = a[0] * b[n ];
a[1] = a[1] * b[n + 1];
Run Code Online (Sandbox Code Playgroud)
总是我的理解是,首先评估"="之后的任何内容,如下所示:
float a, b;
a = b = 5;
//"a" and "b" both equal "5" now.
Run Code Online (Sandbox Code Playgroud)
既然如此,是不是表明我原来的例子应该有效?
任何人都可以解释发生了什么以及为什么这段代码无法正常工作?
我使用++:运算符来获取两个集合的集合,但是我使用这两个方法获得的结果是不一致的:
scala> var r = Array(1, 2)
r: Array[Int] = Array(1, 2)
scala> r ++:= Array(3)
scala> r
res28: Array[Int] = Array(3, 1, 2)
scala> Array(1, 2) ++: Array(3)
res29: Array[Int] = Array(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
为什么++:和++:=运营商会给出不同的结果?++操作员不会出现这种差异.
我使用的Scala版本是2.11.8.
#include <iostream>
using namespace std;
int main()
{
int x=2,a=3,b=2;
x*=a/b;
cout<<x<<" ";
x=2;
x=x*a/b;
cout<<x;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出为:2 3在我看来x * = a / b; 和x = x * a / b; 意思是一样的。有人可以解释这种行为吗?