草庐IT

static成员

全部标签

c++ - 带有显式右值转换运算符的 static_cast

我正在编写一个简单的包装类,我想为包装类型提供显式转换运算符。以下代码使用gcc编译良好classwrap{doublevalue;public:explicitwrap(doublex):value(x){}explicitoperatordouble&&()&&{returnstd::move(value);}};intmain(){wrapw(5);double&&x(std::move(w));//okdouble&&y=static_cast(std::move(w));//clangreportsanerrorhere}但是clang报告error:cannotcastfr

c++ - 使用 unique_ptr 成员编写移动构造函数的正确方法(崩溃)

以下代码在VisualStudio2013下会崩溃我想知道为什么:在这种情况下编写移动构造函数的正确方法是什么?删除移动构造函数解决了这个问题。是VC++的错误还是这段代码有误?移动构造函数的默认定义有何不同,这使得这段代码不会崩溃,而我自己的定义会崩溃?#include#includeclassA{};classFoo{public:Foo(std::unique_ptrref):mRef(std::move(ref)){}Foo(Foo&&other):mRef(std::move(other.mRef)){}Foo(constFoo&other){}Foo&operator=(c

c++ - 非成员函数 begin()/cbegin() 及其constexpr-ness

C++11引入了没有constexpr-说明符的std::begin()非成员函数,然后C++14更新为constexpr-std::begin()用于数组类型(T(&)[N])并附加constexpr-std::cbegin()用于通用容器类型(constC&).引自http://en.cppreference.com/w/cpp/iterator/begintemplateconstexprT*begin(T(&array)[N]);//(sinceC++14)templateconstexprautocbegin(constC&c)->decltype(std::begin(c)

c++ - 使用指向自身的指针初始化成员变量

我听说以下是有效的并且未初始化x就好像它是intx;:intx=x;这个呢?这段代码是否等同于上面的:structFoo{Foo(intFoo::*p):x(this->*p){}intx;};intmain(){Foof(&Foo::x);}f.x是否仍未初始化?我有未定义的行为吗? 最佳答案 C++14清楚地表明使用indeterminatevalueisundefinedbehavior,来自8.5部分(强调我的):Ifnoinitializerisspecifiedforanobject,theobjectisdefault

c++ - 为什么 const 类成员必须是静态的才能得到适当的优化?

给定:classFoo{constintx=5;public:inlineintget(){returnx;}};classBar{staticconstintx=5;public:inlineintget(){returnx;}};intfn0(Foo&f){returnf.get();}intfn1(Bar&b){returnb.get();}编译后的输出提供内存获取以读取fn0()中x的值,而添加static结果是文字5被内联到fn1()中。这意味着只有当整数常量为静态时,get()的调用方才可以像使用常量代替get()一样进行优化。我有更复杂的情况,其中static不合适。派生

c++ - 将具有 unique_ptr 的类的构造函数作为成员复制到抽象类

我有一个类(C),它有一个vector的unique_ptr到一个抽象类(A)作为成员(member)。这是因为C必须与A类型的所有类一起工作,即它的子类。问题是我不知道如何为C编写复制构造函数,因为指针指向的对象的类型在编译时是未知的。在我看来这实际上是不可能的。任何人都可以确认这是不可能的吗?你对如何解决这个问题有什么建议吗?有一个没有复制构造函数的类是不是太糟糕了? 最佳答案 你没有说你是否控制抽象类和从它派生的类的代码。如果这样做,那么最简单的方法是在抽象类中提供一个纯虚方法Clone并在派生类中实现它。此方法应处理创建正确

c++ - 为什么 static const char * template struct 成员没有初始化

我有一些C++11模板代码,我正在尝试移植到VisualC++Compiler2015。原始代码工作得很好,但是我需要重写它以解决constexpr的问题。Theoriginalcode(simplifiedexample)#includestructString{staticconstexprconstchar*value{"STRING"};};templateclassDerived{public:staticconstexprconstchar*value{Base::value};};templatestructFoo{staticconstexprconstchar*val

C++:无法从派生类访问 protected 成员

我有一个MyVariable类,它包含一个对象并在必须修改该对象时做一些额外的工作。现在我想将其专门用于MyContainer,用于容器对象,这些容器对象仅在容器本身被修改(例如通过push_back())而不是其元素时执行此额外工作。我的代码是这样的:templateclassMyVariable{public://read-onlyaccessiffineconstT*operator->()const{return(&this->_element);}constT&operator*()const{return(this->_element);}//writeaccesviath

c++ - protected 成员不能通过指针或对象访问

我有2个类Training和Testing,其中Training是基类,Testing是派生类训练类。我有Testing类成员函数,floattotalProb(Training&classProb,Training&total),它有2个参数,都是Training类对象。代码:voidTesting::totalProb(Training&classProb,Training&total){_prob=(_prob*((float)(classProb._nOfClass)/total._tnClass));coutfirst基本上这个函数所做的是计算test1(Testing类的一

c++ - 初始化成员函数参数传递的shared_ptr

我希望有人能帮助我如何处理这种情况。这里的问题。我有一个共享库(.so、.dll、.dynlib)。在该库中有某种创建对象的工厂类。例如我的方法是这样的:classRenderSystem{public:intcreateTexture(Texture**texture,...);....}createTextureRenderSystem中的方法类看起来像这样:intcreateTexture(Texture**texture,...){....*texture=new...returnsomeErrorCode;}它正在创建Texture实例并将指针传递给texture范围。我们知