我制作了一个非常简单的程序来尝试将唯一指针和继承融合在一起。但是,它最终以退出代码 11 崩溃,我不知道为什么。任何人都可以解释崩溃的原因吗?
//Counter Class, Base class
class Counter {
public:
virtual int addStuff(int& x)=0;
};
Run Code Online (Sandbox Code Playgroud)
//Derived Class, child class of Counter
class Stuff:public Counter {
public:
virtual int addStuff(int& x) override;
};
Run Code Online (Sandbox Code Playgroud)
//Main function using unique pointers to call addStuff from Stuff class
int main() {
int x = 12;
std::unique_ptr<Stuff> p;
p->addStuff(x);
}
Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多 C++ 代码,并且遇到了两种初始化变量的方法。
方法一:
int score = 0;
Run Code Online (Sandbox Code Playgroud)
方法二:
int score {};
Run Code Online (Sandbox Code Playgroud)
我知道这int score {};会将分数初始化为 0,所以也会int score = 0;
这两者有什么区别?我已经阅读了初始化:括号与等号,但这并没有回答我的问题。我想知道等号和大括号之间有什么区别,而不是括号。在什么情况下应该使用哪一个?
根据标准中的一些引用:
[over.match.funcs]/4
[...] 对于通过 using 声明引入派生类的非转换函数,为了定义隐式对象参数的类型,该函数被认为是派生类的成员。
并考虑以下代码:
#include <iostream>
struct Base {
void show(double) {
std::cout << "0" << std::endl;
}
};
struct Test :Base {
using Base::show; //#1
void show(double) { //#2
std::cout << "1" << std::endl;
}
};
int main() {
Test t;
t.show(1.2);
}
Run Code Online (Sandbox Code Playgroud)
根据我引用的标准,这意味着将类型的隐式对象参数Test作为 #1.
对于#1,声明将是show(Test&,double)。
对于#2,声明将是show(Test&,double)。
所以为了重载解析的目的,每个隐式转换序列 的 与 的#1没有区别#2, 的调用t.show(1.2)会模棱两可,但是#2调用,为什么?如果我遗漏了标准中的某些内容,请纠正我。
考虑以下代码片段:
enum class Bar {
A
};
void foo(Bar) {}
struct Baz {
void foo() {
foo(Bar::A);
}
};
Run Code Online (Sandbox Code Playgroud)
它无法编译,来自 gcc 9.2 的消息是:
:12:19: error: no matching function for call to 'Baz::foo(Bar)'
12 | foo(Bar::A);
|
Run Code Online (Sandbox Code Playgroud)
我不怀疑这是一个错误,因为 clang 10 也失败了。关于这种情况,我有两个问题:
标准在哪里定义了这种重载的行为?
以这种方式指定编译器行为的可能原因是什么?
我正在尝试创建一个设置 LinkedList 根节点的函数。但是,当我运行以下代码时:
\n\n#include <iostream>\nusing namespace std;\n\ntemplate <typename K>\nstruct Node {\n Node<K>* next;\n const K value;\n};\n\ntemplate <typename K>\nNode<K>* root = NULL;\n\ntemplate <typename K>\nvoid SetRoot(const K &key) {\n Node<K> new_node = Node<K> {NULL, key};\n root = &new_node;\n}\n\nint main(int argc, char *argv[])\n{\n Node<int> n1 = Node<int> {NULL, 48};\n SetRoot(n1);\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我在该行收到此错误root = &new_node;:
\n\n\n错误: \xe2\x80\x98=\xe2\x80\x99 令牌根 = &new_node; 之前缺少模板参数;
\n
但是,new_node确实具有 struct 的所有预期参数Node。
我正在使用嵌入式模板化库 etl::queue
https://www.etlcpp.com/queue.html
在etl::queueIS quitvalent到std::queue
为了避免复制,我想将元素实际移动到队列中。
现在我的设置看起来像这样
bool CETLConcurrentQueue<ElementType_T, u32QueueSize>::Add(ElementType_T &&element, const uint32_t u32Timeout)
{
//lock mutex...
queue.push(element);
//do further stuff
}
Run Code Online (Sandbox Code Playgroud)
现在我不使用了,queue.push(std::move(element));因为 element 已经是一个右值引用了
但是,queue.push(element);调用元素复制构造函数(已删除)如何改为调用元素移动构造函数?
为什么std::is_class是false我测试它的参考?
int main() {
struct foo_struct {
int i1;
int i2;
};
std::cout << std::boolalpha << std::is_class<foo_struct>::value << std::endl; // true
std::cout << std::boolalpha << std::is_class<foo_struct&>::value << std::endl; // falae
}
Run Code Online (Sandbox Code Playgroud) 如果在“for 循环”中使用逗号运算符来编写多个控制语句,它如何?我试过
#include <iostream>
using namespace std;
int main() {
for (int x = 0, y = 0; x < 3, y < 4; ++x, ++y) {
cout << x << " " << y << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
似乎只计算最后一个表达式。泰
接受完美转发的参数包的正确方法是什么,以便它可以采用任何类型并简单地转发它们?以下代码适用于常规类型,但不适用于指针类型:
template<typename ...A>
void b(A &&... args)
{}
template<typename ...A>
void a(A &&... args)
{
b(std::forward<A>(args)...);
}
int main() {
// ok
a<int>(5);
// error: cannot bind rvalue reference of type ‘int*&&’ to lvalue of type ‘int*’
int *foo = nullptr;
a<int*>(foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
[编辑] 感谢您的快速而精彩的回复!我过于简化了 - 这是我试图解决的问题的一个更接近的例子:
#include <iostream>
using namespace std;
template<typename F>
struct fun;
template<typename F, typename ...A>
struct fun<F(A...)>
{
void b(A &&... args)
{}
void a(A &&... args)
{
b(std::forward<A>(args)...);
}
};
int main() …Run Code Online (Sandbox Code Playgroud) 该程序初始化一个类的对象并将其作为参数传递给另一个成员函数。当我发表声明时
Address a2=a1;
Run Code Online (Sandbox Code Playgroud)
它显示没有错误。但是当我将参数作为对象时
Employee(int id, string name, Address address);
Run Code Online (Sandbox Code Playgroud)
并调用它使用
Employee e1 = Employee(101,"Nakul",a2);
Run Code Online (Sandbox Code Playgroud)
它显示以下错误。
#include <iostream>
using namespace std;
class Address {
public:
string addressLine, city, state;
Address(string addressLine, string city, string state)
{
this->addressLine = addressLine;
this->city = city;
this->state = state;
}
};
class Employee
{
private:
Address address; //Employee HAS-A Address
public:
int id;
string name;
Employee(int id, string name, Address address) //Here it is showing an error
{
this->id = id;
this->name = name; …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×4
templates ×2
c++17 ×1
class ×1
constructor ×1
for-loop ×1
inheritance ×1
name-lookup ×1
oop ×1
overloading ×1
pointers ×1
reference ×1
struct ×1
type-traits ×1