草庐IT

class_list

全部标签

c++ - C++ 中的 class a() 和 class a = class() 有什么区别?

来自Java和C#世界,一直喜欢用someclassa=someclass();代替someclassa();在C++中初始化一个类变量。但是,我的编译器有时会提示ErrorC2280:Attemptingtoreferenceadeletedfunction它们之间有什么区别吗?哪个更好? 最佳答案 Isthereanydifferencebetweenthem?一个大的:someclassa();isdeclaringafunction!someclassa=someclass();,在C++17'scopyellision之前

c++ - 数组与 std::initializer_list 作为函数参数

我可以通过两种方式编写一个将临时数组(例如{1,2,3})作为参数的函数://usingarraytemplateautofoo1(constT(&t)[N])->void;//usingstd::initializer_listtemplateautofoo2(std::initializer_listt)->void;是否有任何指南可以告诉您哪个更好? 最佳答案 它们是完全不同的东西。还有2或3个其他选择是合理的。templatevoidfoo_a(std::arrayconst&);templatevoidfoo_b(gsl:

c++ - 一个类(class)的规模是多少?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:EmptyclassinC++classClass1{charc;};classClass2{};Class1和Class2的大小是多少?在VC6中,我同时获得了1.有人可以解释一下吗?

c++ - `List x;` 和 `List x()` 有区别吗

标题来自名站C++FAQ作者:编码(marshal)·克莱恩。作者声称以下两个代码示例之间存在差异。SupposethatLististhenameofsomeclass.Thenfunctionf()declaresalocalListobjectcalledx:voidf(){Listx;//Localobjectnamedx(ofclassList)...}Butfunctiong()declaresafunctioncalledx()thatreturnsaList:voidg(){Listx();//Functionnamedx(thatreturnsaList)...}但是

c++ - 在 C++ 中重新定义模板 <class T>

我已经搜索并搜索了我的问题的解决方案,但似乎找不到。我正在使用Code::Blocks,但出现了模板类的重定义错误。这是我的“vectorAux.h”文件:#ifndefvectoraux_h#definevectoraux_h#include#include#includetemplatevoidremoveDup(std::vector&v);templateunsignedseqVectSearch(conststd::vector&v,unsignedfirst,unsignedlast,constT&target);templatevoidwriteVector(consts

c++ - 错误 : no instance of overloaded function "std::make_shared" matches the argument list

查看ApreviousstackQuestionstd:make_sharedvsstd::shared_ptr,我试图在一个uni项目中实现它。这是之前的“问题”:Ican'tthinkofanysituationwherestd::shared_ptrobj(newObject("foo",1));wouldbepreferredtoautoobj=std::make_shared("foo",1);因此我采用了这段代码:std::shared_ptrpT1(newTriangle(pCanvas,30,30,30,60,60,30,255,0,0));并将其修改为这段代码:aut

c++ - std::list<int> 的默认构造函数可以抛出吗?

我(快速)查看了C++标准和在线C++引用,但找不到这个简单问题的答案:可以std::list的默认构造函数吗?扔?如果是这样,为什么会抛出? 最佳答案 简短回答:它可以,但它可以以相当安全的方式实现:默认构造函数构造了一个空列表,因此几乎不需要在进程中实际分配内存。大多数列表实现不会为空列表分配任何内存。但是,默认构造函数不是真正默认构造函数,因为它有一个默认参数:explicitlist(constAllocator&=Allocator());Allocator本身是一个模板参数,因此如果Allocator有一个足够笨(或复杂

c++ - 使用 std::stack 而不是 deque、vector 或 list 的优点和缺点是什么

我正在编写一个非常简单的std::stack,使用vector作为其底层容器。我意识到我可以用vector容器的push_back()、pop_back()和back()替换所有的push()、pop()和top()函数。我的问题是:当底层容器的受控使用就足够时,为什么还要使用容器适配器?为什么不只使用双端队列、vector或列表?会不会浪费内存或处理时间? 最佳答案 当您的代码显示std::stack时,读者很清楚他们需要在容器上执行哪些操作……它在强制不使用其他操作的同时进行通信和记录。它可以帮助他们快速形成对代码中算法逻辑的印

c++ - SFINAE 专业类(class)

我想编写一个模板类,它使用SFINAE检查特征。正如我在那篇文章中读到的那样,类不能被“重载”:templateoverloadingandSFINAEworkingonlywithfunctionsbutnotclasses我写了下面的代码:classAA{public:usingTRAIT=int;};classBB{public:usingTRAIT=float;};templateclassX;templateclassX::value,int>::type>{public:X(){std::coutclassX::value,unsignedint>::type>{publi

c# - 父类(super class)构造函数中的虚拟化

我认为根据OOP的设计,虚拟化在父类(superclass)构造函数中不起作用。例如,考虑以下C#代码。usingSystem;namespaceProblem{publicclassBaseClass{publicBaseClass(){Console.WriteLine("Hello,World!");this.PrintRandom();}publicvirtualvoidPrintRandom(){Console.WriteLine("0");}}publicclassDescendent:BaseClass{privateRandomrandomValue;publicDes