我有一个类(那些已经读过Accelerated C++的人可能会发现这个类很熟悉)定义如下:
class Student_info{
public:
Student_info() : midterm(0.0), final(0.0) {};
Student_info(std::istream& is){read(is);};
Student_info(const Student_info& s);
~Student_info();
Student_info& operator=(const Student_info& s);
//Getters, setters, and other member functions ommited for brevity
static int assignCount;
static int copyCount;
static int destroyCount;
private:
std::string name;
double midterm;
double final;
double finalGrade;
std::vector<double> homework;
};
typedef std::vector<Student_info> stuContainer;
bool compare(const Student_info& x, const Student_info& y);
Run Code Online (Sandbox Code Playgroud)
函数calculator()使用这种类型的对象.作为函数的一部分,使用库的通用排序函数对(已声明的)Student_info对象的向量进行排序.我的程序没有超过这一点(尽管根据NetBeans没有抛出任何异常并且程序正确退出).
sort函数大量使用容器中保存的任何类型的赋值运算符,但我似乎无法找出我定义的那个错误(程序在我定义之前正常运行).根据Accelerated C++(或至少这是我解释它的方式),赋值运算符应该工作的正确方法是首先销毁左操作数,然后使用等于右操作数的值再次构造它.所以这是我的重载operator =定义:
Student_info& Student_info::operator=(const Student_info& s)
{
if(this != &s)
{
this->~Student_info();
destroyCount++;
*this = s;
}
return …Run Code Online (Sandbox Code Playgroud) 似乎initalizer列表对于类构造函数来说是一个好主意,而且我假设,对于复制构造函数也是如此.对于赋值运算符,必须在函数体中分配每个成员.考虑以下简单块:
class Foo {
private:
int a,b;
public:
Foo(int c, int d) : a(c), b(d) {}
Foo(const Foo & X) : a(X.a), b(X.b) {}
Foo& operator=(const Foo& X) {
if (this == &X) return *this;
a = X.a;
b = X.b;
return *this;
}
};
Run Code Online (Sandbox Code Playgroud)
如果一个类具有适量的数据成员,则有三个地方可以搞乱不同的分配/初始化.我的意思是,如果复制构造函数看起来像:
Foo(const Foo & X) : a(X.a), b(X.a) {}
Run Code Online (Sandbox Code Playgroud)
或者运营商缺少一条线=.由于赋值运算符和复制构造函数通常具有相同的效果(因为我们将成员从一个Foo复制到另一个Foo),我可以"重用"复制构造函数或赋值运算符中的代码,反之亦然吗?
假设我没有operator=在类中指定复制构造函数,以下两个声明之间有什么区别Beatle?
Beatle john(paul);
和
Beatle john = paul;
编辑:
在对象赋值中,=除非另有说明,否则运算符会隐式调用复制构造函数?
我正在实现一个非常轻的原子包装器作为C++ for Windows中原始数据类型的学习练习,我有一些关于实现赋值运算符的简单问题.考虑以下两种实现:
// Simple assignment
Atomic& Atomic::operator=(const Atomic& other)
{
mValue = other.mValue;
return *this;
}
// Interlocked assignment
Atomic& Atomic::operator=(const Atomic& other)
{
_InterlockedExchange(&mValue, other.mValue);
return *this;
}
Run Code Online (Sandbox Code Playgroud)
假设这mValue是正确的类型,并且Atomic类将其作为成员.
_InterlockedExchange需要线程安全的赋值运算符,或者是否足以保证线程安全的简单实现?_InterlockedExchange保证其他平台上的线程安全性?我正在研究C ++编程语言,正在阅读有关赋值运算符(=)的章节。在C ++中,初始化和赋值操作非常相似,因此我们可以使用相同的符号。
但是我的问题是:当我初始化变量时,我是否正在使用赋值运算符进行操作?当我分配一个变量时,我是否使用赋值运算符来完成它?我认为唯一的区别是初始化和赋值之间的区别,因为当我们初始化一个变量时,我们使用assignmnet运算符为其赋予了一个新值,当我们将其赋给一个变量时,便使用了新值来替换该变量的旧值。赋值运算符。这样对吗 ?
字符串文字在C#中是否具有特定类型(如c ++中的const char*),或者C#是否只为程序中出现的每个字符串文字创建一个新的字符串对象?我很想知道在执行以下语句时幕后发生的事情:
string s1 = "OldValue";
Run Code Online (Sandbox Code Playgroud)
这会调用字符串类中的特定方法(构造函数,或者一个impicit转换操作符,......)或C#是否创建一个包含"OldValue"的新字符串对象(然后只是将其引用分配给s1,就像它会对于任何参考类型)?
我试图理解字符串类的设计是什么,保证s1的价值仍为"OldValue":
string s2 = s1;
s2 = "NewValue";
Run Code Online (Sandbox Code Playgroud) 考虑多重分配x[0],y = y,x[0].应用于以下四种情况中的每一种情况,这给出了四种不同的结果.
情况1:
x = [[1,2], [3,4]]
y = [5,6]
Run Code Online (Sandbox Code Playgroud)
给
x = [[5,6], [3,4]]
y = [1,2]
Run Code Online (Sandbox Code Playgroud)案例2:
x = np.array([[1,2], [3,4]])
y = [5,6]
Run Code Online (Sandbox Code Playgroud)
给
x = array([[5,6], [3,4]])
y = array([5,6])
Run Code Online (Sandbox Code Playgroud)案例3:
x = [[1,2], [3,4]]
y = np.array([5,6])
Run Code Online (Sandbox Code Playgroud)
给
x = [array([5,6]), [3,4]]
y = [1,2]
Run Code Online (Sandbox Code Playgroud)案例4:
x = np.array([[1,2], [3,4]])
y = np.array([5,6])
Run Code Online (Sandbox Code Playgroud)
给
x = array([[5,6], [3,4]])
y = array([5,6])
Run Code Online (Sandbox Code Playgroud)看起来列表的多重分配比Numpy阵列的多重分配更智能(自动通过临时变量).
思考?
编辑:事后并不聪明......
NaN是可疑起源的残留实现之一,但在大多数情况下我得到它.但是,我今天将它输入到Node提示符中并且无法理解它...
NaN = !NaN
> true
Run Code Online (Sandbox Code Playgroud)
这只是简单地返回评估结果!NaN吗?这是有道理的,但我很惊讶尝试将NaN分配给另一个值时没有错误.
注意:这个问题是关于这个特定的语法结构; 有很多关于NaN和isNaN的问题,但我在google搜索后找不到答案.感谢Ori Drori 迄今为止的最佳答案.
console.log(NaN = !NaN);Run Code Online (Sandbox Code Playgroud)
我已经参考了这个链接 - > https://gist.github.com/Taymindis/3938e917aaae4fc480386f494be62f0e并做了一些valgrind检查,它没有错误.但我只想双重确认以下这个例子考虑线程安全吗?
我个人阴道检查,它没有错误,有没有人有更好的主意?
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#define THREAD_RUN 100
static char *global;
static char *x1 = "This is thread 1";
static char *x2 = "This is thread 2";
void * write(void* thr_data) {
int n = *(int*)thr_data;
if(n%2==0) goto RETRY1;
else goto RETRY2;
RETRY1:
for (n = 0; n < 1000; ++n) {
global = x1;
}
goto RETRY1;
RETRY2:
for (n = 0; n < 1000; ++n) {
global = x2; …Run Code Online (Sandbox Code Playgroud) 假设我有list参数的功能,并且在它的正文中我想修改传递列表,通过以下代码:
spy = [0,0,7]
def replace_spy(lista):
return lista[2]=lista[2]+1
Run Code Online (Sandbox Code Playgroud)
但它告诉我错误: SyntaxError: invalid syntax