草庐IT

const_cast-ing

全部标签

c# - InvalidCastException : Unable To Cast Objects of type [base] to type [subclass]

我有一个继承自MembershipUser的自定义CustomMembershipUser。publicclassConfigMembershipUser:MembershipUser{//customstuff}我正在使用Linq-to-SQL从数据库中读取并获取用户实体;为了使此功能成为MembershipUser,我定义了一个显式转换:publicstaticexplicitoperatorMembershipUser(Useruser){DateTimenow=DateTime.Now;if(user==null)returnnull;returnnewMembershipUs

c++ - const 成员函数优先于返回值类型匹配

在Y::test1()中,非constX::operatorvoid*()优先于看似更好的匹配,X::operatorbool()const-为什么?标准中对这种现象的描述在哪里?#includestructX{operatorvoid*(){std::cout输出:test1()operatorvoid*()test2()operatorbool() 最佳答案 首先:将return语句中的表达式转换为函数的返回类型时,规则与初始化相同(参见[conv]/2.4和[conv]/3).所以我们可以使用这个示例来检查代码的行为(使用与您

c++ - 从 const unordered_map 读取对象

为什么不允许我从常量unordered_map中读取对象?constunordered_mapz;intval=z[5];//compileerrorclang下的错误如下:error:noviableoverloadedoperator[]fortype'constunordered_map'intval=z[5];考虑到使用constvector的等效代码可以正常工作,我有点困惑为什么会出现这种行为。 最佳答案 表达式z[5]调用映射的非常量成员函数。这是因为map的operator[]会在没有找到键的情况下插入一个新元素,所以

javascript - 如何在 try catch block 中使用 const

这个问题在这里已经有了答案:Javascriptsetconstvariableinsideofatryblock(7个回答)关闭5年前。const是一个block级变量,所以当我尝试可疑代码时try{constfoo=bar[global.name].foofoo[global.name2];}catch(err){console.log(error(err.message));}const隐藏在{}但是constfoo;try{foo=bar[global.name].foofoo[global.name2];}catch(err){console.log(error(err.me

kotlin - 处理这种情况的最佳方法是 "smart cast is imposible"

我想知道处理这种情况的最佳方法是什么classPerson(varname:String?=null,varage:Int?=null){funtest(){if(name!=null&&age!=null)doSth(name,age)//smartcastimposible}fundoSth(someValue:String,someValue2:Int){}}调用doSth方法并确保name和age为ntnull的最简单方法是什么?我正在寻找一些简单的东西,比如我会简单地使用let的可变场景name?.let{doSth(it)} 最佳答案

c++ - 移动构造函数应该采用 const 还是非 const 右值引用?

在几个地方,我看到推荐的复制和移动构造函数签名如下:structT{T();T(constT&other);T(T&&other);};复制构造函数采用const引用,而移动构造函数采用非const右值引用。据我所知,这阻止了我在从函数返回const对象时利用移动语义,例如以下情况:Tgenerate_t(){constTt;returnt;}使用VC11Beta进行测试,调用的是T的复制构造函数,而不是移动构造函数。即使使用returnstd::move(t);仍然会调用复制构造函数。我明白这是怎么回事,因为t是const所以不应该绑定(bind)到T&&。在移动构造函数签名中使用c

javascript - 在类构造函数中定义一个 const (ES6)

这个问题在这里已经有了答案:DeclaringstaticconstantsinES6classes?(18个答案)关闭6年前。有没有办法在类的构造函数中定义一个const?我试过这个:classFoo{constructor(){constbar=42;}getBar=()=>{returnthis.bar;}}但是vara=newFoo();console.log(a.getBar());返回未定义的。

c++ - 为什么我需要从 `long &` 到 `int &` 的 reinterpret_cast,两者都是 32 位 (Windows LLP64)?

我在跨平台环境中使用Qt。我们遇到了以下问题:在Windows上,int和longint都是32位整数;在64位MacOS和Linux上,int是32位的,longint是64位的(参见https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models)。因此,跨平台库倾向于提供它们自己的固定位类型定义。在Windows上,Qt将quint32定义为unsignedint并且不使用unsignedlong整数。另一个库将其Uint32定义为unsignedlong。因此,两者实际上都是32位无符号整数,但具有不同的原始数据

php - 使用 static::MY_CONST vs self::MY_CONST vs SomeClass::MY_CONST 引用 PHP 7 中的常量

我想一劳永逸地讲清楚。我很确定我知道何时使用self::MY_CONST和SomeClass::MY_CONST但不清楚何时使用static::MY_CONST.Youuseself::MY_CONST……当您引用常量在您调用它的同一个类中定义时。例子:classFoo{constMY_CONST=123;publicfunctionexample(){echoself::MY_CONST;}}YouuseAnotherClass::MY_CONST……当你引用一个常量在你调用它的不同类中定义。例子:classBar{constMY_CONST=123;}classFoo{public

c++ - 如何将带有 const 参数的 C++ 函数或 C++ 结构链接到 D 可执行文件?

我正在尝试将C++目标文件链接到D可执行文件。dlang.org建议这应该是可能的:http://dlang.org/cpp_interface.html事实上,为我想链接到的C++对象文件创建D接口(interface)文件非常简单。C++模块的公开部分只是一组全局C风格函数和一些结构。我想做的应该是这样的:rdmd-main-unittest-L-lcurl-Lsvm.o"-L/usr/lib/libstdc++.a"svmWrapper.d其中svm.o是C++目标文件,而svmWrapper.d是构建为可执行文件的d文件(在本例中,只是一个将运行单元测试的文件)。但是,对于在D