草庐IT

c++ - 从 unique_ptr<char[]> 到 unique_ptr<const char[]>

移动unique_ptr的最佳成语是什么?到unique_ptr?用例:假设您在某个缓冲区中创建了一个C字符串。为确保在出现异常时进行正确清理,可以使用unique_ptr引用该缓冲区.构造字符串后,您可能希望将其移动到某个类成员,声明为unique_ptr。以避免进一步修改字符串。这是我迄今为止最好的:std::unique_ptrres;std::unique_ptrbuf(newchar[4]);buf[0]='f';buf[1]=buf[2]='o';buf[3]='\0';res=std::unique_ptr(const_cast(buf.release()));单纯的移动

c++ - 如何返回线交叉点?线并不总是相交

假设我们想要创建一个函数来计算两条线的交点。交点并不总是定义的或唯一的。如何在函数的签名中反射(reflect)这一点?我想出了这些选项:boolgetIntersectionPoint(Line&a,Line&b,Point&result);如果直线平行则返回false。否则返回true并将结果写入变量。PointgetIntersectionPoint(Line&a,Line&b);如果直线平行则抛出异常。[更新]如果我们制作2个函数booldoLinesIntersect(constLine&,constLine&);和PointtwoLinesIntersection(cons

c++ - 无法从派生类访问基类中的 protected 成员

这是我的代码:#include#include#includeusingnamespacestd;classroot{protected:intsize;double*array;public:virtual~root(){}virtualroot*add(constroot&)=0;virtualroot*sub(constroot&)=0;virtualistream&in(istream&,root&)=0;virtualintgetSize()const=0;virtualvoidsetSize(int);};classaa:publicroot{public:aa();aa(

c++ - 为什么在 const 引用生命周期是当前范围的长度时使用 const 非引用

因此在C++中,如果将函数的返回值分配给const引用,则该返回值的生命周期将是该引用的范围。例如MyClassGetMyClass(){returnMyClass("someconstructor");}voidOtherFunction(){constMyClass&myClass=GetMyClass();//lifetimeofreturnvalueisuntiltheend//ofscopeduetomagicconstreferencedoStuff(myClass);doMoreStuff(myClass);}//myClassisdestructed因此,无论您通常将函

c++ - 如何初始化const循环引用成员

比如我有两个类classFoo;classBar;classFoo{constBar&m_bar;...};classBar{constFoo&m_foo;...};让foo是Foo的对象和bar是Bar的对象.有什么方法(正常或“黑客”)来创建/初始化foo和bar他们的成员m_bar和m_foo会互相引用(我的意思是foo.m_bar是bar而bar.m_foo是'foo')?允许添加任何成员到Foo和Bar,为他们添加parent,使他们成为模板等等。 最佳答案 foo和bar的联系是什么?如果他们有外部链接,你可以这样写:e

c++ - 使用模板赋值运算符

有以下代码,为什么第一个赋值不调用Foo中的模板operator=,而第二个赋值呢?这里发生了什么?即使存在用户定义的模板,是否有编译器为第一次赋值生成模板?#includeusingnamespacestd;structUberFoo{};structFoo:publicUberFoo{templatevoidoperator=(constT&v){coutvoidoperator=(T&v){cout 最佳答案 编译器仍在生成第一个赋值绑定(bind)到的非模板operator=。在第二个作业中,模板化的operator=是一个

c++ - 只为容器提供 const 迭代器有意义吗?

我有一个类似于下一个的容器:classMySpecialContainer{std::vector>_vec;};哪里Type1和Type2可在容器外使用,InternalType仅在容器内使用。为了从外部迭代元素,我使用了一个类似于下一个的成员函数:voidMySpecialContainer::iterate(std::functionfun){for(auto&it:_vec){fun(std::get(it),std::get(it));}}如您所见,这种方法有几个局限性,比如不能迭代子范围或不能使用非变异std::algorithms.考虑MySpecialContainer

c++ - C++ STL 容器中的多态性

我想知道是否存在解决此问题的优雅解决方案。假设如下:classBase{private:intdata;protected:Base(intdata):data(data){}intgetData()const{returndata;}virtualboolsomeOp()=0;}classDerived1:publicBase{private:intdata2;public:Derived1(intdata,intdata2):Base(data),data2(data2){}intgetData2()const{returndata2;}boolsomeOp()override{/

c++ - 为什么C++允许从外部修改一个常量对象的指针成员变量的内存?

当我在C++中编写一个带有常量参数和该对象内部的指针变量的函数时,我一直在努力理解,而不是const标志不保护底层内存免受修改。例如,在名为X的类的operator=()函数中执行以下操作是完全合法的:classX{public:X&operator=(constX&other){this->data=other.data;//(*)return*this;}private:int*data;};(*):这与以下相同:int*some_pointer;int*constother_pointer=some_pointer;int*class_pointer=other_pointer;

opencv - libopencv_calib3d : undefined reference to `std::__throw_out_of_range_fmt(char const*, …)@GLIBCXX_3.4.20'

我提到了this在我的RaspberryPi2上安装OpenCV(它运行在最新的Raspbian上,内核版本为4.1.7-v7)。由于依赖项错误,我无法安装libgtk2.0-dev,但我能够毫无错误地安装OpenCV。我正尝试在Qt中为我的RaspberryPi2交叉编译一些简单的OpenCV代码。但是我在链接器阶段遇到以下错误:/usr/local/lib/libopencv_calib3d.so:undefinedreferencetostd::__throw_out_of_range_fmt(charconst*,...)@GLIBCXX_3.4.20我的代码是:myFunc{