草庐IT

static_pointer_cast

全部标签

c++ - 如何避免 const cast 进行 map 访问?

我有以下问题:std::mapmap;voidgetColor(Aconst*obj){doubled=map[obj];//doesnotcompilewihtoutconst_cast(obj)//dosomething}我有一个mapstd::map(某处),它存储指向对象A的指针。我有一个getColor函数,它不操作对象A,因此将指向constA的指针作为输入.如果不使用const_cast,函数getColor将无法编译。constcast是一个设计问题,但如果我不想在map中制作键,我不知道如何规避它常量。任何帮助表示赞赏。 最佳答案

c++ - 在 C++ 中,static_cast<double>(a) 和 double(a) 有什么区别?

两者有什么区别inta;//agetssomevaluedoublepi=static_cast(a)/3;和inta;//agetssomevaluedoublepi=double(a)/3;你见过后者吗?在我看来,我在Stroustrup编写的一些片段中看到了它,但我找不到引用。 最佳答案 可能有人认为他们是在构建而不是在类型转换。考虑:some_fun(std::string("Hello"));许多人认为他们在那里调用了构造函数,而实际上他们正在执行C风格的转换。碰巧的是,强制转换会在它查看的一长串其他东西中查看目标类型的构

c++ - 在 C++ 中,static_cast<double>(a) 和 double(a) 有什么区别?

两者有什么区别inta;//agetssomevaluedoublepi=static_cast(a)/3;和inta;//agetssomevaluedoublepi=double(a)/3;你见过后者吗?在我看来,我在Stroustrup编写的一些片段中看到了它,但我找不到引用。 最佳答案 可能有人认为他们是在构建而不是在类型转换。考虑:some_fun(std::string("Hello"));许多人认为他们在那里调用了构造函数,而实际上他们正在执行C风格的转换。碰巧的是,强制转换会在它查看的一长串其他东西中查看目标类型的构

c++ - 使用static_assert时如何避免没有返回表达式的警告?

我有两个库可以使用,并且为了方便起见,我在它们使用的一些类型/结构之间编写了一个转换器。templatestructunsupportedType:std::false_type{};templateFormatBgetFormat(){static_assert(unsupportedType::value,"Thisisnotsupported!");}templateFormatBgetFormat(){returnFormatB(//someparameters);}templateFormatBgetFormat(){returnFormatB(//someotherpara

c++ - 使用static_assert时如何避免没有返回表达式的警告?

我有两个库可以使用,并且为了方便起见,我在它们使用的一些类型/结构之间编写了一个转换器。templatestructunsupportedType:std::false_type{};templateFormatBgetFormat(){static_assert(unsupportedType::value,"Thisisnotsupported!");}templateFormatBgetFormat(){returnFormatB(//someparameters);}templateFormatBgetFormat(){returnFormatB(//someotherpara

c++ - 什么时候应该在非成员函数之前写关键字 'static'?

我最近在SO上看到了一些关于函数前的static关键字的信息,我想知道如何正确使用它。1)什么时候应该在非成员函数前写关键字static?2)在header中定义静态非成员函数是否危险?为什么(不)?(附带问题)3)是否可以以某种方式在头文件中定义一个类,以便它只在您首先使用它的翻译单元中可用?(我问这个的原因是因为我正在学习STL,它可能是我的谓词等(可能是仿函数)的一个很好的解决方案,因为我不喜欢定义成员以外的函数-cpp文件中的函数)(另外,我认为它在某种程度上与原始问题相关,因为根据我目前的推理,它会在函数之前做与static相同的事情)编辑看到一些答案时提出的另一个问题:4)

c++ - 什么时候应该在非成员函数之前写关键字 'static'?

我最近在SO上看到了一些关于函数前的static关键字的信息,我想知道如何正确使用它。1)什么时候应该在非成员函数前写关键字static?2)在header中定义静态非成员函数是否危险?为什么(不)?(附带问题)3)是否可以以某种方式在头文件中定义一个类,以便它只在您首先使用它的翻译单元中可用?(我问这个的原因是因为我正在学习STL,它可能是我的谓词等(可能是仿函数)的一个很好的解决方案,因为我不喜欢定义成员以外的函数-cpp文件中的函数)(另外,我认为它在某种程度上与原始问题相关,因为根据我目前的推理,它会在函数之前做与static相同的事情)编辑看到一些答案时提出的另一个问题:4)

c++ - 我不明白为什么这个函数 "returns a pointer from the list"

我正在读的书,IntroductiontoDataStructureswithLinkedLists(Presentation21),有2个链表示例。这是第一个:EnemySpaceShip*getNewEnemy(){EnemySpaceShip*p_ship=newEnemySpaceShip;p_ship->x_coordinate=0;p_ship->y_coordinate=0;p_ship->weapon_power=20;p_ship->p_next_enemy=p_enemies;p_enemies=p_ship;returnp_ship;}链表的第二个例子是这个:En

c++ - 我不明白为什么这个函数 "returns a pointer from the list"

我正在读的书,IntroductiontoDataStructureswithLinkedLists(Presentation21),有2个链表示例。这是第一个:EnemySpaceShip*getNewEnemy(){EnemySpaceShip*p_ship=newEnemySpaceShip;p_ship->x_coordinate=0;p_ship->y_coordinate=0;p_ship->weapon_power=20;p_ship->p_next_enemy=p_enemies;p_enemies=p_ship;returnp_ship;}链表的第二个例子是这个:En

c++ - static const std::map<string, int> vs if-elseif

我编写了一个将字符串转换为数字的函数。我看到了两种可能的写法:intconvert(conststd::stringinput){if(input=="one"){return1;}elseif(input=="two"){return2;}//etc.return0;}或者intconvert(conststd::stringinput){staticconstmaptable={{"one",1},{"two",2}//etc.}constautoresult=table.find(input);if(result==table.end()){return0;}returnresu