草庐IT

c++ - C++ 标准是否允许非模板类的模板构造函数?

我想创建一个带有模板构造函数的类:classfoo{templatefoo(Tvar){}};这在VS2008中编译,但我不知道它是否是非标准扩展,或者C++标准是否允许它。 最佳答案 在[temp.mem]中:Atemplatecanbedeclaredwithinaclassorclasstemplate;suchatemplateiscalledamembertemplate.构造函数是成员,并没有明确禁止将它们设为模板。例如,std::shared_ptr有很多templateconstructors.

c++ - 具有独占继承构造函数的类的值初始化

根据cppreference没有任何用户提供的构造函数的非union类类型将在构造之前被零初始化:IfTisannon-unionclasstypewithoutanyuser-providedconstructors,thentheobjectiszero-initializedandthentheimplicitly-declareddefaultconstructoriscalled(unlessit'strivial)我不确定在使用c++11继承的构造函数时会发生什么,因为引文明确提到了隐式声明默认构造函数。给定以下示例:#includestructA{inta;A(){}A(

c++ - 在类的洋葱中搜索

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭4年前。给定一个整数模板参数Key,搜索方法应该在编译期间提取一层具有key==Key的洋葱。如何修复搜索方法?测试(也在godbolt.org)#includeclassA{public:staticconstexprintkey=0;templateconstexprauto&search()const{return*this;}};templateclassB{public:staticconstexprintkey=P:

c++ - 如何与模板化类的构造函数成为 friend ?

为什么classA;templateclassB{private:A*a;public:B();};classA:publicB{private:friendB::B();intx;};templateB::B(){a=newA;a->x=5;}intmain(){return0;}结果../src/main.cpp:15:error:invaliduseofconstructorasatemplate../src/main.cpp:15:note:use‘B::B’insteadof‘B::classB’tonametheconstructorinaqualifiedname还在改变

c++ - 在另一个类的构造函数中初始化类对象数组

如果我有一个类:classA{private:charz;intx;public:A(charinputz,intinputx);~A(){}}我想在B类中创建一个A数组。classB{private:Aarrayofa[26];public:B();~B(){}voidupdatearray();//Thiswillfillthearraywithwhatisneeded.}classB{B:B(){updatearray();std::sort(&arrayofa[0],&arrayofa[26],A::descend);}}如何在B的构造函数中显式初始化arrayofa?

c++ - 为什么我不能调用派生自的模板类的模板化方法

structMessages{templatestaticconstchar*message(){return"testmesage";}};templatestructTest:publicM{Test(){M::message();//error:expectedprimary-expressionbefore'int'}};intmain(){Testt;}我怀疑这与一些相互依赖有关,比如Test的代码依赖于基类M,其方法在Test中是专门的。这是正确的吗? 最佳答案 M::message是一个从属名称,因为M是一个模板参数。

c++ - 通过初始化列表调用另一个类的构造函数。有问题

这是我的示例代码:#includeusingnamespacestd;classBase{public:Base(intv,charz){x=v;y=z;};intx;chary;};classBar{public:Bar(intm,charn):q(m),s(n),base(q,s){};Basebase;intq;chars;};intmain(){BarbarObj(5,'h');cout为什么我得到的输出是0?http://ideone.com/pf47j另外,一般来说,在另一个类中创建成员对象并调用该对象的构造函数的正确方法是什么,就像上面对classBase的对象base所

c++ - 在类的构造函数中初始化由 vector 组成的矩阵

我正在尝试构建一个具有字符矩阵的游戏。我正在尝试使用vector的vector来构建我的矩阵。我的game.h有这个:#ifndefGAME_H#defineGAME_H//includesusingnamespacestd;classGame{private:introw;intcol;vector>*matrix;//otheratributtespublic:Game();~Game(){}//somefunctions};#endif在我的game.cpp中:Game::Game(){this->col=20;this->row=20;//Initializethematrix

c++ - 如何使用另一个类的对象为类编写复制构造函数

我在编写这个相当简单的程序时遇到了麻烦。我有两个类A和B。B有一个A的对象。我需要编写B的Copy构造函数,以便B的两个实例将具有不同的A实例。有没有什么巧妙的方法可以做到这一点?要做的一件事是获取parm的所有成员变量,创建一个新的A对象并分配这些成员变量。但是如果这个类有更多的成员变量,那就是个问题了。如何简单地写这个?classA{public:intdata;A(){}A(intparm):data(parm){}A(constA&parm){this->data=parm.data;}A&operator=(constA&parm){if(this!=&parm){this-

c++ - 为什么不能直接定义匿名结构/类的模板化别名?

我可以创建以下内容:usingFoo=struct{/*Implementation*/};templateusingBar=Foo;但是以下是不允许的:templateusingBar=struct{/*Implementation*/};Clang的错误比GCC更有用,它指出:error:'(anonymousstructatfile:line:column)'cannotbedefinedinatypealiastemplate不允许第二个代码示例的任何原因?注意:请说明第二个代码示例(如果允许)可能导致语言问题的任何示例。标准中的任何引用也很有帮助。