草庐IT

inheriting-constructors

全部标签

C++ "No appropriate default constructor available"

我想在不使用STL的情况下创建一个数组链表。但是,我在将数组传递到我的链接列表时遇到困难...编译时出现上面列出的错误。我需要如何将数组传递给链表?谢谢!(有问题的代码有**标记,如果测试请去掉)单链表.h#pragmaonce#ifndefSinglyLinkedList_h#defineSinglyLinkedList_h#includetemplatestructnode{Typevalue;node*next;};templateclassSinglyLinkedList{private:node*head;public:SinglyLinkedList();~SinglyLi

c++ - "Inheriting"具有可变模板函数的类

我想编写一个可以操作不同类型数据库(例如sqlite、postgres等)的数据库包装器,因此无论用户实际使用什么数据库,他们编写的代码都不会改变。在我看来,这需要一个抽象基类,例如:classdatabase{public:virtualboolquery(conststd::string&q)=0;//Otherstuff};classsqlite:publicdatabase{public:boolquery(conststd::string&q){//Implementation}};这看起来不错,但我正在使用可变参数模板来转义查询中的参数(我真的很喜欢这个想法,所以我想坚持下

c++ - 类 std::array of objects without default constructors

所以让我们假设我有以下类(class)classNoDefaultConstructor{NoDefaultConstructor()=delete;...};我还有另一个类,它有一个类型为NoDefaultConstructor和其他成员的数组classWrapper{std::arrayarr;...};如何在Wrapper的构造函数中初始化数组(可能在使用std::intializer_list的初始化列表中)?更具体地说,是我可以将参数传递给Wrapper的初始化列表中的数组构造函数以具有类似于以下的构造的唯一方法吗?我正在考虑这样做,因为将来数组的大小可能会发生变化。temp

c++ - 转发声明 : templates and inheritance

在编写框架时遇到以下问题:我有classA和classB派生自classA。classA有一个返回B*的函数。当然,这并不难:#includeusingnamespacestd;classB;//forwarddeclarationclassA{public:B*ReturnSomeData();};classB:publicA{};//Implementation:B*A::ReturnSomeData(){returnnewB;//doesn'tmatterhowthefunctionmakespointer}intmain(){Asth;cout但是我不得不使用像这里这样的模板:

c++ - "member of type foo has private copy constructor"错误 : why's it an error?

我试图定义这样一个类:#includeclassmy_class{private:someone_elsesfoo;public:myclass();~myclass();//...};但是编译器失败了:“someone_elses类型的字段foo有一个私有(private)的复制构造函数”。现在我知道我可以通过以下方式解决这个问题:classmy_class{private:someone_elses*foo;//...};my_class::my_class(){foo=newsomeone_elses();}my_class::~my_class(){deletefoo;}我的问

c++ - 继承构造函数 vs 转发

C++11允许继承构造函数,从而可以避免大量样板文件,尤其是使用包装类之类的东西。但是,您似乎已经可以单独使用可变参数模板实现此功能。classB{public:B(int){//dosomething}B(int,char){//dosomething}};使用继承构造函数:classD:publicB{public:usingB::B;};使用可变模板和转发:classD:publicB{public:templateD(Args&&...args):B(std::forward(args)...){}};虽然一致性(对于using以相同的方式对待构造函数和方法)和易用性是将继承的

c++ - 使用 move-constructor 时将 self 重置为 nullptr 是个好习惯吗?

在C++11中,移动构造函数/运算符支持资源/内存移动。这是我的例子:classA{public:A():table_(nullptr),alloc_(0){}~A(){if(table_)delete[]table_;}A(constA&other){//table_isnotinitialized//if(table_)//delete[]table_;table_=newint[other.alloc_];memcpy(table_,other.table_,other.alloc_*sizeof(int));alloc_=other.alloc_;}A&operator=(co

c++ - 在 Constructor works 中将临时变量传递给引用 arg。但不是一般的功能。为什么?

这个问题在这里已经有了答案:Defaultconstructorwithemptybrackets(9个回答)关闭7年前。考虑以下代码。在这里,即使构造函数是A(B&b),Aa(B())也会编译;但是print(B())不起作用。但是print也被声明为print(B&b);为什么会出现这种不一致?#includeusingnamespacestd;classB{public:charb;};classA{public:Bb;A(B&b);A(){}};A::A(B&b){this->b=b;}voidprint(B&b){}intmain(){print(B());Aa(B());}

c++ - C++ 代码错误 "expected constructor, destructor, or type conversion before ‘(’ token ”和 "no matching function for call to ..."

真正尝试解决错误,仔细检查所有内容。请帮忙。c++新手,请多关照。头文件(.h)#ifndefGUARD_Optimized_quick_sort_h#defineGUARD_Optimized_quick_sort_h#include#include#includeusingnamespacestd;templateclassoptimized_quick_sort{public:optimized_quick_sort(vectorarray){this->array=array;}optimized_quick_sort(listarray){vectortemp(array.b

C++ 单例用法 : compiler complains about private constructor

我知道有一百万个关于单例的问题和答案,但我似乎无法找到解决方案。所以冒着反对票的风险,这是我的问题:我想使用AndreiAlexandrescu的现代C++设计中的单例实现:标题:classSingleton{staticSingleton&Instance();private:Singleton(){};Singleton(constSingleton&){};Singleton&operator=(constSingleton&){};~Singleton(){};};实现:#include"s.hh"Singleton&Singleton::Instance(){staticSi