编辑:我在这里重新问了同样的问题(解决了这个问题后面的问题):为什么这个C++ 0x程序会产生意外的输出?
基本的想法是,如果你不小心,指向可移动的东西可能会给你带来一些奇怪的结果.
C++移动构造函数和移动赋值运算符看起来非常积极.它们可以在复制构造函数没有意义的情况下使用,因为它们不需要重复指向的资源.
但有些情况下,如果你不小心,他们会咬你.这是特别相关的,因为我已经看到允许编译器生成移动构造函数的默认实现的建议.如果有人能给我一个,我会提供一个链接.
所以,这里有一些代码有一些可能不完全明显的缺陷.我测试了代码,以确保它使用-std=gnuc++0x标志以g ++编译.这些缺陷是什么?你会如何修复它们?
#if (__cplusplus <= 199711L) && !defined(__GXX_EXPERIMENTAL_CXX0X__)
#error This requires c++0x
#endif
#include <unordered_set>
#include <vector>
#include <utility>
#include <algorithm>
class ObserverInterface {
public:
virtual ~ObserverInterface() {}
virtual void observedChanged() = 0;
virtual void observedGoingAway() = 0;
};
class Observed {
private:
typedef ::std::unordered_set<ObserverInterface *> obcontainer_t;
public:
Observed() {}
Observed(const Observed &) = delete;
const Observed &operator =(const Observed &b) = delete;
// g++ does not currently support defaulting the …Run Code Online (Sandbox Code Playgroud) EI具有作为向量的参数指针的函数:
void Function(std::vector<type>* aa)
Run Code Online (Sandbox Code Playgroud)
现在在这个函数里面我想过滤掉从那个向量到另一个向量的数据,我想通过改变这个临时值的值来改变原始向量的数据.该死的很难理解:
void Function(std::vector<type>* aa)
{
std::vector<type*> temp; //to this vector I filter out data and by changning
//values of this vector I want to autmatically change values of aa vector
}
Run Code Online (Sandbox Code Playgroud)
我有类似的东西:
void Announce_Event(std::vector<Event>& foo)
{
std::vector<Event> current;
tm current_time = {0,0,0,0,0,0,0,0,0};
time_t thetime;
thetime = time(NULL);
localtime_s(¤t_time, &thetime);
for (unsigned i = 0; i < foo.size(); ++i) {
if (foo[i].day == current_time.tm_mday &&
foo[i].month == current_time.tm_mon &&
foo[i].year == current_time.tm_year+1900)
{
current.push_back(foo[i]);
}
}
std::cout …Run Code Online (Sandbox Code Playgroud) 所以我正在编写这个程序来执行一个基本任务菜单,其中一个是告诉用户输入的字符是大写,小写还是不是字母.
#include <iostream>
using namespace std;
int main () {
int mi;
cout << "1) Area of Circle" << endl;
cout << "2) Character Detection" << endl;
cout << "3) Capitalization 1-3-5" << endl;
cout << "4) Binomial Roots" << endl;
cout << "0) Quit" << endl;
cin >> mi;
switch (mi) {
case 2:
{
char c;
cout << "input a character: ";
cin.get(c);
cin.ignore(); /////// unsure if using this properly
if ('a' <= c && c <= 'z') …Run Code Online (Sandbox Code Playgroud) project1.cpp
#include "stdafx.h"
#include "Bicycle.cpp"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
bool runP = true;
do {
Bicycle object();
char oType;
cout << "Would you like a (B)icycle, or (A)nimal? E for Exit\n";
cin >> oType;
if (oType == 'B' || oType == 'b') {
int seat, wheels;
string brand;
cout << "How many wheels does the bike have?\n";
cin >> wheels;
object().setWheels(wheels);
cout << "How many seats does the bike have?\n";
cin >> seat;
object().setSeats(seat);
cout …Run Code Online (Sandbox Code Playgroud) 当我看到虚拟虚空enter()= 0时,我正在读书。赋值不是变量时该怎么办?
class MapSite {
public:
virtual void Enter() = 0;
};
Run Code Online (Sandbox Code Playgroud)