草庐IT

Initialization

全部标签

c++ - 使用 Int 的初始化列表初始化 Long Double 的 vector

假设我有一个简单的类:classPvector{private:std::vectorpoint_list;public:Pvector(std::initializer_listcoords):point_list(coords){}Pvector(std::initializer_listcoords):point_list(coords){}};这将无法编译,因为longdouble模板化的std::vector无法从int类型模板化的初始化列表中初始化自身.然而,这很不方便,因为删除了第二个构造函数后,我无法在我的代码中执行以下操作:Pvectorpiece_movement(

c++ - 在 C++98 中初始化结构数组的优雅方式

我正在使用gcc版本4.9.2如果我使用编译器标志-std=c++0x进行编译,则以下代码编译正常。#include#includeusingnamespacestd;typedefstruct{vectora;intb;}MYTYPE;intmain(void){MYTYPEtest[]={{{1,2,3},4},{{5,6},7},{{},8}};}如果我删除-std=c++0x标志,编译器会报告:error:couldnotconvert‘{1,2,3}’from‘’to‘std::vector’什么是初始化test[]的优雅方式? 最佳答案

c++ - 避免使用虚方法构造具有空基类的构造函数

我有一个带有虚函数的空基类。有什么方法可以避免手动实现Derived的构造函数以便能够对其进行初始化?structBase{virtualintf(int)const=0;virtual~Base()=0;};Base::~Base(){}structDerived:publicBase{intv;intf(int)constoverride{returnv;};};intmain(){returnDerived{5}.f(3);} 最佳答案 IsthereanywayIcanavoidmanuallyimplementingthe

C++ 模板静态成员指针初始化

我有一个模板类,它有一个指向成员的静态指针,如下所示:templateclassQueue{T*head;T*tail;staticT*T::*pnext;};我的问题是如何编写指向成员的静态指针的初始化程序。我尝试了明显的情况:templateT*Queue::*pnext(nextptr);但这没有用。有什么想法吗? 最佳答案 你真的需要一个与模板参数具有相同值的模板静态成员变量吗?唯一的用途是它的值在程序的整个生命周期内发生变化,但我真的想不出任何情况会产生比造成困惑更多的好处。

c++ - 在 C++ 函数头中初始化变量

我遇到过一些看起来像这样的C++代码(针对本文进行了简化):(这里是位于someCode.hpp中的函数原型(prototype))voidsomeFunction(constdouble&a,double&b,constdoublec=0,constdouble*d=0);(这里是函数体的第一行位于#include的someCode.hpp的someCode.cpp)voidsomeFunction(constdouble&a,double&b,constdoublec,constdouble*d);我可以合法地调用someFunction使用:someFunction(*ptr1,

c++ - 为什么我们不能在算法和数据结构中使用引用?

Asageneralrule,usereferencesinfunctionparametersandreturntypestodefineattractiveinterfaces.Usepointerstoimplementalgorithmsanddatastructures.我在一篇文章中看到了这一行。我有疑问为什么不能在算法和数据结构中使用引用?请帮我理解这是为什么。 最佳答案 因为在初始化为引用特定对象后,无法引用任何其他对象。作为证据,标准的§8.5.3.2说Areferencecannotbechangedtorefe

c++ - POD 与非 POD 类类型的默认初始化

C++标准说(8.5/5):Todefault-initializeanobjectoftypeTmeans:IfTisanon-PODclasstype(clause9),thedefaultconstructorforTiscalled(andtheinitializationisill-formedifThasnoaccessibledefaultconstructor).IfTisanarraytype,eachelementisdefault-initialized.Otherwise,theobjectiszero-initialized.用这段代码structInt{in

c++ - 如何 "track"缺少 ctor 初始化列表参数?

我敢肯定,每个人都会时不时地遇到丑陋的事情。问题是向类中添加了一个字段而忘记了扩展初始化列表,例如:classT{private:field1;...field10;};T::T(intspeedValue):field1(Speed::MphToMps(speedValue)),field2(newOtherClass(14,5,15)),field3(PublicValueGenerator::generateNewFieldValue(0,15)),...,field10("unpredictablevalue");如果赶时间,我会添加一些字段,例如newForgottenFie

c++ - 错误 : initialization with "{...}" expected for aggregate object - c++

structtest{unsignedinttest1;unsignedchartest2[4096];unsignedinttest3;}foostructfoobar{unsignedchardata[4096];}如果我想访问该结构,我会说foo.test1、foo.test2[4096]等。但是,当我希望以下列方式返回foo.test2中存在的数据时pac.datafoo=foo.test2[4096];unsignedchardata[4096]=pac.datafoo;这是我得到的错误:error:initializationwith"{...}"expectedforag

c++ - 正在为 C++11 之前的编译器返回线程安全的本地静态对象

对于C++11之前的编译器来说,这个单例线程安全吗?正如我们所知,对于C++11,它是线程安全的。classSingleton{private:Singleton(){};public:staticSingleton&instance(){staticSingletonINSTANCE;returnINSTANCE;}}; 最佳答案 在C++11中,使该线程安全的是来自draftC++11standard的以下内容6.7部分声明语句说(强调我的):Thezero-initialization(8.5)ofallblock-scope