草庐IT

const_object

全部标签

c++ - 是否可以在成员例程中使用 const_cast 来避免重复代码

在那种情况下可以使用const_cast还是有任何注意事项:classA{public:A():m_someData(5){}int&get(){returnm_someData;};constint&get()const{const_cast(this)->get();};private:intm_someData;};这样做的目的是让get例程可能更加复杂,并且应该避免代码重复。 最佳答案 没有。我不建议那样做。我建议您反向使用const_cast:int&get(){returnconst_cast(const_cast(*t

c++ - 指向 const 指针(或指向 const 的指针)

我有这个代码:#includeintmain(){intv1=20;int*p1=&v1;int**p2=&p1;return0;}我想在这里做的是将一个指针指向另一个指针,在这种情况下它工作正常。我将p1设为const指针或指向const的指针:int*constp1=&v1;ORconstint*p1=&v1;现在我的代码不起作用。考虑第一种情况。int*constp1=&v1;。这是我更新后的代码,我认为应该是正确的:#includeintmain(){intv1=20;int*constp1=&v1;constint**p2=&p1;return0;}使指针本身成为const意

c++ - "C++ compilers use a binary object layout"这句话的含义和用途是什么

在浏览此C++常见问题解答时https://isocpp.org/wiki/faq/mixing-c-and-cpp#cpp-objs-passed-to-c我遇到了语句MostC++compilersuseabinaryobjectlayoutthatcausesthisconversiontohappenwithmultipleinheritanceand/orvirtualinheritance.我无法理解它的含义和应用。根据C++FAQ,此对象布局机制有助于C++编译器进行以下检查InC++itiseasytocheckifaDerived*calleddppointstoth

c++ - 访问成员变量中的引用会丢弃 constness

我在我的代码中围绕一个对象做了一个包装,它应该修改对该对象的访问。我选择在这里使用一个对象来进行测试,而不是使用具有相同功能的仿函数。基本上:包装器接收对对象的引用并将所有索引访问转发到对象(在一些可能的操作之后)现在问题来了:访问器丢弃了被包装对象的常量性。最小的例子structFoo{std::arraydata;constint&operator()(intidx)const{returndata[idx];}int&operator()(intidx){returndata[idx];}};structBar{Foo&ref;Bar(Foo&r):ref(r){}int&ope

c++ - const 或 ref 或 const ref 或 value 作为 setter 函数的参数

恒常性classMyClass{//...private:std::stringm_parameter;//...}按值传递:voidMyClass::SetParameter(std::stringparameter){m_parameter=parameter;}通过引用:voidMyClass::SetParameter(std::string¶meter){m_parameter=parameter;}通过常量引用:voidMyClass::SetParameter(conststd::string¶meter){m_parameter=parameter;}按

C++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案

c++ - 类 std::array of objects without default constructors

所以让我们假设我有以下类(class)classNoDefaultConstructor{NoDefaultConstructor()=delete;...};我还有另一个类,它有一个类型为NoDefaultConstructor和其他成员的数组classWrapper{std::arrayarr;...};如何在Wrapper的构造函数中初始化数组(可能在使用std::intializer_list的初始化列表中)?更具体地说,是我可以将参数传递给Wrapper的初始化列表中的数组构造函数以具有类似于以下的构造的唯一方法吗?我正在考虑这样做,因为将来数组的大小可能会发生变化。temp

c++ - 如何将低级常量应用于模板变量。我正在尝试编写一个 const_cast 实现

如何让这个static_assert传递到我失败的代码中?我尝试了T周围const的所有排列,但我无法获得constint*。编译器总是将其解释为int*const。templateunionconst_cast_impl{usingCT=constT;static_assert(std::is_same::value,"CTisnotconstint*");Tdata;CTcdata;const_cast_impl(CTptr):cdata(ptr){}operatorT(){returndata;}};intmain(){inta=2;constint*ptr=&a;int*ptr

c++ - 如何像 C++ const/constexpr 一样定义 CUDA 设备常量?

在.cu文件中,我在全局范围内尝试了以下操作(即不在函数中):__device__staticconstdoublecdInf=HUGE_VAL/4;并得到nvcc错误:error:dynamicinitializationisnotsupportedfor__device__,__constant__and__shared__variables.如果可能的话,如何在设备上定义C++const/constexpr?注意1:#define是不可能的,不仅出于美学原因,而且因为在实践中表达式更复杂并且涉及内部数据类型,而不仅仅是double。因此,每次在每个CUDA线程中调用构造函数的代价

c++ - 从 const string 到 bool 的隐式转换

这个问题在这里已经有了答案:WhyisthereanimplicittypeconversionfrompointerstoboolinC++?(3个答案)关闭5年前。我有以下代码:#include#includevoidfoo(boola){std::cout执行时我得到这个输出:bool我期望的输出是:string为什么g++4.9将此字符串隐式转换为bool?