在类的赋值运算符中,通常需要检查所分配的对象是否是调用对象,这样就不会搞砸了:
Class& Class::operator=(const Class& rhs) {
if (this != &rhs) {
// do the assignment
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
移动赋值运算符需要相同的东西吗?是否有过这样的情况this == &rhs?
? Class::operator=(Class&& rhs) {
?
}
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
struct foo
{
int a;
};
foo q() { foo f; f.a =4; return f;}
int main()
{
foo i;
i.a = 5;
q() = i;
}
Run Code Online (Sandbox Code Playgroud)
没有编译器抱怨它,甚至Clang.为什么q() = ...线路正确?
#include <iostream>
using namespace std;
void printarray (int arg[], int length) {
for (int n = 0; n < length; n++) {
cout << arg[n] << " ";
cout << "\n";
}
}
int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray(firstarray, 3);
printarray(secondarray, 5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码有效,但我想了解数组是如何传递的.
当printarray从main函数调用函数时,正在传递数组的名称.数组的名称是指数组的第一个元素的地址.这等同于int arg[]什么?
我一直试图找到一种方法让Clang在Windows上工作,但遇到了麻烦.我让Clang成功编译,但是当我尝试编译程序时,我在标准头文件中有一堆错误.
我知道rubenvb的优秀预装版本的clang,但我想为自己编译它.我也在听GoingNative谈论clang,它说它还没有很好的Windows支持.我如何让Windows在Windows上工作?
在我阅读的过程中,我遇到了1993年写的WG14缺陷报告#51(或许是1893年,他们离开了世纪和千年).在那里的代码示例中,显然->>在指向a的指针上使用了拼写的运算符struct.我在我发现的任何运算符优先级表中都找不到它,所以我想知道,是或者它曾经是一个运算符,如果是这样的话,这个运算符会做什么(或者视情况而定)?
起初我认为这是一个拼写错误,但它在文本中再现了两次,而在代码示例中再次再现了问题,我很难相信它只是滑过至少两位C专家而不是当它像我这样的新手跳出来的时候注意到了.它也是代码的焦点,非常容易注意到,并且从未得到纠正.
这是添加了缩进的代码:
#include <stdlib.h>
struct A {
char x[1];
};
main()
{
struct A *p = (struct A *) malloc(sizeof(struct A) + 100);
p->>x[5] = '?'; /* This is the key line [for both them and us] */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用C和C++编译器编译此代码,但无法解析任何一个.也许这是C的早期版本中的某些运算符,不再使用了?
这感觉就像这个运营商的名字一样可疑:" - >"?问题,但我不认为这是其他两个运营商的组合,我不知道它是如何划分和有效的.
我试图制作一个Panel可滚动的,但只是垂直(所以AutoScroll不会工作,因为子控件超过左边缘,必须).
那怎么办?
我尝试在IdeOne(使用gcc 4.5.1)和我的Linux计算机(使用类似4.6.4)上编译这个简单的程序:
#include <string>
#include <iostream>
int main() {
std::cout << std::stoi("32") << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它完美地编译并输出32.但是,当我尝试使用MinGW和gcc 4.6.1在我的Windows计算机上编译它时,我收到此错误:
test.cpp: In function 'int main()':
test.cpp:5:19: error: 'stoi' is not a member of 'std'
Run Code Online (Sandbox Code Playgroud)
同样的事情发生在std::stoul等等.std::stoi由于某种原因,MinGW中不存在和家庭吗?我认为MinGW(sh | w)上的gcc与Linux上的行为相同.
是否可以拥有相同类的多个版本,这些版本仅在模板参数的数量上有所不同?
例如:
template<typename T>
class Blah {
public:
void operator()(T);
};
template<typename T, typename T2>
class Blah {
public:
void operator()(T, T2);
};
Run Code Online (Sandbox Code Playgroud)
我正在尝试模拟仿函数类型的东西,它可以采用可变数量的参数(最多可写出不同模板的数量).
很抱歉,如果之前已经询问过,但据我所知,在C++ 11中,std::vector有一个移动构造函数,因此在某些情况下复制成本几乎没有,比如按值返回一个.但是,如果我有这样的类,使用vector作为成员变量:
class MyClass {
public:
MyClass() { }
MyClass(const MyClass& rhs) { }
// other interfaces
private:
std::vector<int> myvec;
// implementation
};
Run Code Online (Sandbox Code Playgroud)
并且有一个函数可以按值返回其中一个,例如
MyClass somefunc() {
MyClass mc;
// fill mc.myvec with thousands (maybe even millions) of ints
return mc;
}
Run Code Online (Sandbox Code Playgroud)
即使本身对移动构造函数一无所知,是否mc.myvec会调用移动构造函数和std::vector利用MyClass移动构造函数?或者是否vector要调用复制构造函数,并且int必须逐个复制所有数千个(甚至数百个)的复制构造函数?
这可能是一个明显答案或重复的问题.如果有,抱歉,我会删除它.
为什么不复制构造函数(如默认ctors或dtors),以便在调用派生类的复制构造函数之前调用基类的复制构造函数?对于复制构造函数和析构函数,它们分别在从base-to-derived和derived-to-base的链中被调用.为什么复制构造函数不是这种情况?例如,这段代码:
class Base {
public:
Base() : basedata(rand()) { }
Base(const Base& src) : basedata(src.basedata) {
cout << "Base::Base(const Base&)" << endl;
}
void printdata() {
cout << basedata << endl;
}
private:
int basedata;
};
class Derived : public Base {
public:
Derived() { }
Derived(const Derived& d) {
cout << "Derived::Derived(const Derived&)" << endl;
}
};
srand(time(0));
Derived d1; // basedata is initialised to rand() thanks to Base::Base()
d1.printdata(); // prints the random number
Derived d2 = …Run Code Online (Sandbox Code Playgroud)