草庐IT

const-reference

全部标签

c++ - std::unique_ptr 转移 const 对象的所有权

我有一个A类的对象在方法内部创建。此方法还创建对象的实例B将对象A作为构造函数参数刚刚创建。B必须取得对象的所有权A但它不能修改它。这意味着AB时应删除被删除,但在B的生命周期内它不能修改A.在本例中为std::unique_ptr作为B的成员变量是转移A所有权的正确方法(在std::move的构造函数中使用B)并保证它不会被修改? 最佳答案 是的,这正是您正在寻找的语义。std::unique_ptr声明“我拥有T对象。”指向constA的指针(原始或智能)声明“我无法修改我指向的A”。总而言之,这正是您所追求的。

c++ - 对 WinMain@16 C++、SDL-2 的 undefined reference

我一直收到错误undefinedreferencetoWinMain@16。为了节省空间,here'salinktoallthefilescurrentlyintheproject.目前,除了创建一个窗口、将其填充为绿色然后在角落绘制一个框外,它应该做的不多,同时通过控制台跟踪鼠标的位置。但是,它不会构建,并且出现上述错误。我的链接器库是:glew32slibSDL2mainmingw32libSDL2opengl32glew32我正在使用Codeblocks13.12和g++,遵循C++11ISOC++语言标准。如果相关的话,我的电脑使用的是Windows10。我花了很长时间试图找到

c++ - std::optional<std::reference_wrapper<T>> - 可以吗?

是std::optional>是否符合C++17的标准(或草案)?标准明确指出,引用类型的std::optional格式错误。但它是否包括reference_wrapper? 最佳答案 是的。那没问题。它不包括reference_wrapper因为reference_wapper不是引用类型。只有实际的引用类型是不允许的。 关于c++-std::optional>-可以吗?,我们在StackOverflow上找到一个类似的问题: https://stackov

c++ - shared_ptr 中对 const int 的 undefined reference

我有一个配置类//config.hppclassConfig{public:staticconstexprinta=1;staticconstexprintb=1;}并包含在main.cpp中//main.cpp#include"config.hpp"intmain(){std::coutstream=std::make_shared(Config::a);//compileerror}编译器说未定义对Config::a的引用它在使用cout时有效,但在shared_ptr构造函数中时无效。我不知道为什么会这样。 最佳答案 请注意s

c++ - 声明为 consts 的函数 args 随定义变为非常量

这个问题在这里已经有了答案:Whydoesafunctiondeclarationwithaconstargumentallowcallingofafunctionwithanon-constargument?(4个答案)关闭4年前。代码classA{public:voidf(constinti);};voidA::f(inti){std::cout为什么编译器在这种情况下不报错?为什么定义会覆盖常量参数?此外,当参数的类型为reference(&)时,编译器会抛出错误,但在这种情况下为什么不呢?是否有任何编译器标志可以对这些提到的情况发出警告?我对编译器错误POV更感兴趣。因为可以很

c++ 何时返回 const char* 而不是 std :string

在下面的类中,speak()返回constchar*而不是std::string有什么好处或原因吗?classAnimal{protected:std::stringm_name;Animal(std::stringname):m_name(name){}public:std::stringgetName(){returnm_name;}constchar*speak(){return"???";}}; 最佳答案 std::string带有许多可能不需要和未使用的功能。如果您不想要所有这些功能,您应该考虑使用成本。传递std::st

c++ - 为什么要给小类成员返回一个 const 引用?

我觉得这个问题肯定在某个地方,但要么我找不到正确的搜索词,要么不知何故遗漏了。假设我有这样保护其成员的类......classMyClass{intm_value;public:MyClass(intv):m_value(v){}intvalue()const{returnm_value;}}我已经看到遍及SO的示例代码,而是像这样返回对成员变量的const引用......classMyClass{intm_value;public:MyClass(intv):m_value(v){}constint&value()const{returnm_value;}//^^^^^^^^^^}我

c++ - 不同类型的 `decltype((const int)a)` 和 `decltype((const int)1)`

//g++7.3templatestructtd;intmain(){inta=1;tdt1;tdt2;return0;}编译结果如下:错误:聚合‘tdt1’类型不完整,无法定义错误:聚合‘tdt2’类型不完整,无法定义那么,为什么decltype((constint)a)的类型是和decltype((constint)1)不一样? 最佳答案 说明符decltype((constint)a)和decltype((constint)1)都解析为int。这是因为没有const非类类型的纯右值,如C++17[expr]中所述:Ifaprv

c++ - 为什么 const vector<const pair<...>> 给出 'cannot be overloaded' 错误?

我有这个简单的代码:#include#includevoidfoo(conststd::vector>&networks){for(autop:networks){}}voidbla(conststd::vector>&networks){for(autop:networks){}}这会在bla()中产生一个错误:mrvn@frosties:~%g++-O2-W-Wall-g-std=gnu++17-cbla.ccInfileincludedfrom/usr/include/x86_64-linux-gnu/c++/5/bits/c++allocator.h:33:0,from/usr

c++ - 首选哪个,return const double& OR return double

给定以下场景,以下哪一项是首选。m_state是一个成员评价者,而不是局部变量。classC{private:doublem_state;public:doublestate()const{returnm_state;}//returnsdoubledouble&state(){returnm_state;}}===========================================classC{private:doublem_state;public:constdouble&state()const{returnm_state;}//returnsconstdouble&