在下面的代码中,有什么方法可以demo在使用std::make_unique()分配demo[]数组时将参数传递给构造函数?
class demo{
public:
int info;
demo():info(-99){} // default value
demo(int info): info(info){}
};
int main(){
// ok below code creates default constructor, totally fine, no problem
std::unique_ptr<demo> pt1 = std::make_unique<demo>();
// and this line creates argument constructor, totally fine, no problem
std::unique_ptr<demo> pt2 = std::make_unique<demo>(1800);
// But now, look at this below line
// it creates 5 object of demo class with default constructor
std::unique_ptr<demo[]> pt3 = std::make_unique<demo[]>(5);
// but I need here …Run Code Online (Sandbox Code Playgroud) 在我的 C++ 程序中,我创建了两个重载函数 doSomething() 和 doSomething(int, int)。我的问题是,当我通过传递 (async, doSomething, a, b) 参数调用异步任务时,编译器会给出“调用异步没有匹配函数”错误。那么伙计们,我如何在异步任务下传递两个 arg doSomething(int, int) 函数。
我的代码:
void doSomething(){ // do some work}
void doSomething(int &a, int &b){ // do some work}
int main(){
int a = 34, b = 44;
auto f = std::async(std::launch::async, doSomething, std::ref(a), std::ref(b));
}
Run Code Online (Sandbox Code Playgroud) 我的代码:
int num = 1; // global scope
int main(){
int num = 2; // local scope 1
{ // local scope 2
int num = 3;
{ // local scope 3
int num = 4;
std::cout<<num<<"\n"; // printing local scope 3
std::cout<<::num<<"\n"; // printing global scop
// but here how to print local scope 1, 2 variables
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码中的人有嵌套的作用域,我想从“本地作用域 3”打印所有具有相同名称的变量,包括阴影变量。但是,我可以打印全局和局部范围 3 的 num 值,但我不知道访问局部范围 1 和 2 的 num 值的语法。