想象一下管理资源的以下类(我的问题只是关于移动赋值运算符):
struct A
{
std::size_t s;
int* p;
A(std::size_t s) : s(s), p(new int[s]){}
~A(){delete [] p;}
A(A const& other) : s(other.s), p(new int[other.s])
{std::copy(other.p, other.p + s, this->p);}
A(A&& other) : s(other.s), p(other.p)
{other.s = 0; other.p = nullptr;}
A& operator=(A const& other)
{A temp = other; std::swap(*this, temp); return *this;}
// Move assignment operator #1
A& operator=(A&& other)
{
std::swap(this->s, other.s);
std::swap(this->p, other.p);
return *this;
}
// Move assignment operator #2
A& operator=(A&& other)
{
delete [] p; …
Run Code Online (Sandbox Code Playgroud) 所以,我有一个类存储指向对象的指针.我有一个方法,可以向对象添加对象.添加时,我知道我可以通过引用或指针传递,并阅读了每个的优点和缺点,但在这种情况下,我无法弄清楚哪一个更好,为什么.对于我可以弄清楚的一切,他们几乎是一样的(但我可能错了!)
这是(一个释义)传递指针/地址:
hpp:
class Room {
vector<Item*> items;
public:
void addItem(Item*);
};
cpp:
void Room :: addItem(Item* item) {
items.push_back(item);
}
Run Code Online (Sandbox Code Playgroud)
......并通过引用传递:
hpp:
class Room {
vector<Item*> items;
public:
void addItem(Item &);
};
cpp:
void Room :: addItem(Item &item) {
items.push_back(&item);
}
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个?
std::vector<int> v1;
std::function<void(const int&)> funct =
static_cast<std::function<void(const int&)>>(std::bind(
&std::vector<int>::push_back,
&v1,
std::placeholders::_1));
Run Code Online (Sandbox Code Playgroud)
这给了我no matching function for call to bind
.
我能做到这一点:
threadPool.push_back(std::thread(runThread<lazyFunct>,
std::bind(&LazySkipList<int>::add,
&lsl, std::placeholders::_1), 0, 1000000, 1000000));
Run Code Online (Sandbox Code Playgroud)
我正在使用Xcode 4.4和llvm C++ 11支持.
例
#include <vector>
#include <cassert>
template <typename Cont, typename... Rest>
void f(Cont& c, Rest&... rest)
{
assert(c.size() == ???);
}
int main()
{
std::vector<int> v1(10);
std::vector<int> v2(10);
std::vector<int> v3(10);
std::vector<int> v4(10);
f(v1, v2, v3, v4);
}
Run Code Online (Sandbox Code Playgroud)
我想确保传递给函数的所有容器都是相同的大小.但是,该函数是一个可变参数模板,它采用相同类型的任意数量的容器.这可能吗?
例
template <typename T>
struct A
{
typedef A<T> super;
};
template <typename T>
struct B : A<T>
{
B() : super() {} // <-- HERE
};
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
使用MSVC编译器,它按原样编译.但是对于gcc,我需要super()
改为A<T>::super()
.我假设gcc在这里是正确的,但有人可以帮我理解这里的规则以及哪个编译器在技术上是正确的?
IDEONE:http: //ideone.com/ uSqSq7
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
struct node
{
int value, position;
bool left, right;
bool operator < (const node& a) const
{
return value < a.value;
}
};
int main()
{
int n;
cin >> n;
vector < node > a(n);
set < node > s;
for (auto &i: a)
{
cin >> i.value;
i.left=i.right=0;
}
a[0].position=1;
s.insert(a[0]);
for (int i=1; i<n; i++)
{
auto it=s.upper_bound(a[i]);
auto it2=it; --it2; …
Run Code Online (Sandbox Code Playgroud) 我想重载一个强制转换操作符,我有以下代码:
template <typename _T>
class CTest
{
public :
_T data;
CTest(_T _data) : data(_data) {}
~CTest() {}
operator _T(){ return data; }
};
Run Code Online (Sandbox Code Playgroud)
和MS Visual Studio 2005,给我以下错误:
警告C4003:宏" T"警告的实际参数不够C4003:宏' _T'错误的实际参数不足 C2833:'运算符L'不是识别的运算符或类型请参见类模板实例化'CTest <_T>'正在编译错误C2059:语法错误:'newline'错误C2334:'{'之前的意外标记; 跳过明显的函数体错误C2833:'运算符L'不是可识别的运算符或类型
如何正确声明operator _T()?
我正在使用C++并使用TDateTime数据类型.
如何获取当前系统日期/时间并将其放入TDateTime变量?
请考虑以下示例:
#include <iostream>
#include <clocale>
#include <cstdlib>
#include <string>
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
std::string s = "03A0";
wchar_t wstr = std::strtoul(s.c_str(), nullptr, 16);
std::wcout << wstr;
}
Run Code Online (Sandbox Code Playgroud)
这?
在Coliru输出.
题
std::strtoul
,是来自<cstdlib>
.使用它我完全没问题,但我想知道上面的例子是否只能使用C++标准库(也许是字符串流)?
另请注意,0x
字符串上没有prefex 表示十六进制.
示例代码:
#include <iostream>
int main()
{
if(int a = std::cin.get() && a == 'a')
{
std::cout << "True" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
当我编译这段代码时,visual studio给了我一个很好的警告:warning C4700: uninitialized local variable 'a' used
.所以我明白这a
是未初始化的.但是,我想完全理解如何评估表达式.上面的if语句是否等同于if(int a && a == 'a') { a = std::cin.get(); }
?有人能解释到底发生了什么?
我正在学习C++而且我遇到过这个,我不明白这个小东西.为什么GetName()函数是字符类型的指针,为什么它是常量?
class Derived: public Base
{
public:
Derived(int nValue)
: Base(nValue)
{
}
const char* GetName() { return "Derived"; }
int GetValueDoubled() { return m_nValue * 2; }
};
Run Code Online (Sandbox Code Playgroud) 假设我有以下课程:
class Test
{
int num;
public:
Test(int x):num(x){}
Test(const Test &rhs):num(rhs.num+1){}
};
int main()
{
Test test(10);
Test copy = test;
}
Run Code Online (Sandbox Code Playgroud)
将num
在副本应该是11
,我的问题是关于拷贝构造函数里面,我们为什么不能访问私有成员num
的test
使用num
来初始化num
的副本?令我困惑的是,如果你输入cout<<test.num<<endl
,当然这是错的,因为你试图访问私有num
,但如果你通过参考复制构造函数传递测试,它可以工作,任何人都可以告诉我这里发生了什么?
这行(x = n&5;)在下面的代码中是什么意思?据我所知,&符用于指针.我不希望编译这个代码,但它编译并运行良好.我得到的结果是
0,1,0,1,4,5,4,5,0,1,
#include <stdio.h>
int main(void){
int x, n;
for (n = 0; n < 10; n++){
x = n & 5;
printf("%d,", x);
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)