a std::pair和std::tuple只有两个成员之间有区别吗?(除了明显std::pair需要两个,只有两个成员,tuple可能会少...)
这个方法:
bool Point::Intersects(const Line& line) const {
return (line.ContainsPoint(*this, false));
}
Run Code Online (Sandbox Code Playgroud)
导致此错误:无法将'this'指针从'const Line'转换为'Line&'此更改:
bool Point::Intersects(const Line& line) const {
return const_cast<Line&>(line).ContainsPoint(*this, false);
}
Run Code Online (Sandbox Code Playgroud)
修复错误,但似乎不是解决问题的正确方法.为什么原始方法被认为是错误?
如果它有帮助,ContainsPoint(const Point& point, bool isInfinite)是非const,它调用的所有方法也是非const.
在调试和发布模式之间切换时,此代码在MSVS 2012,Windows 7中生成不同的值:
#include <iostream>
using namespace std;
int A[20000];
int main() {
int shift = 0;
int Period = 30;
//Fill array
for(int i = 0; i < 20000; i++) {
A[i] = i * 2 + 123;
}
int sumTotal = 0;
int sum = 0;
for(int bars = Period + 10; bars < 1000; bars++) {
sum = 0;
for(int i = 0; i< Period; i++) {
sum += A[bars - i];
}
sumTotal += sum;
} …Run Code Online (Sandbox Code Playgroud) c++ visual-c++ compiler-bug visual-studio-2012 visual-studio-2013
摘要
有没有办法在模板化类型上调用类方法,该模板可能是指针或引用而不知道哪个而不是获取编译器/链接器错误?
细节
我有一个模板化的QuadTree实现,可以采用以下任何非平凡的用户定义类型:
//Abstract Base Class
a2de::Shape
//Derived Classes
a2de::Point
a2de::Line
a2de::Rectangle
a2de::Circle
a2de::Ellipse
a2de::Triangle
a2de::Arc
a2de::Spline
a2de::Sector
a2de::Polygon
Run Code Online (Sandbox Code Playgroud)
但它们可能是指针或引用,因为它们都是从a2de :: Shape派生的.因此专业化被声明为:
template class QuadTree<a2de::Shape&>;
//...similar for all derived types as references.
template class QuadTree<a2de::Shape*>;
//...similar for all derived types as pointers
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是当间接(或缺少)未知时调用类方法的能力,并且由于模板,生成了两组代码:
template<typename T>
bool QuadTree<T>::Add(T& elem) {
//When elem of type T is expecting a pointer here
//-> notation fails to compile where T is a reference i.e.:
//template class QuadTree<a2de::Shape&>
//with "pointer to reference is illegal" …Run Code Online (Sandbox Code Playgroud) 是否适合使用throw new FoobarException(Baz argument);或throw FoobarException(Baz argument);?
在捕捉时我总是使用catch(FoobarException& e)"以防万一",但我无法找到一个可靠的答案,我是否必须在C++中使用新的或不使用(肯定是Java),或者它只是程序员的偏好.
与使用原始指针的类似(但不限于)某些高级技术相比,每个智能指针的等效用途是什么?
我的理解很少,但我可以收集到:
std::shared_ptr::reset底层时deallocator.std::shared_ptr不增加引用计数的子类型,当其父级std::shared_ptr不再存在时无效.可能会返回无效的引用.使用前请务必检查.RAW指针等效示例
引用计数,缓存实现: std::map<std::string, std::pair<long, BITMAP*> > _cache;
拥有所有权的单身人士:
class Keyboard {
public:
//...
static Keyboard* CreateKeyboard();
~Keyboard();
//...
private:
//...
Keyboard();
static Keyboard* _instance;
//...
};
Run Code Online (Sandbox Code Playgroud)
聚合容器,无所有权:空间分区图和树,迭代容器等.
复合容器,所有权:大型对象.
- 编辑 -
在我工作的时候,我遇到了一个有趣的案例,DeadMG指出智能指针应该被用作简单的抽象来处理资源管理; 那些在声明点无法在堆上创建但必须在以后创建的文件范围对象呢?
根据Scott Meyers的说法,为了防止在const版本的getter和非const版本的getter中重复代码,请从非const版本调用该方法的const版本:static_cast<const A&>(*this).Methodology(); 但是,由于过度热心而意外使用我输入的Visual Assist X Intellisense:const_cast<const A&>(*this).Methodology();它运行得很好.
在这种情况下使用特定演员阵容有什么区别?
正在使用的IDE:Visual Studio 2010.
感谢Microsoft为Atomineer Utils的Intellisense和Atomineer ...所有这些参数都是必需的且不可变的.
有一个更好的方法吗?
/**************************************************************************************************
* <summary>Initializes a new instance of the ADTBattleCharacter class.</summary>
* <param name="name"> The name of the character.</param>
* <param name="max_HP"> The maximum hit points.</param>
* <param name="max_MP"> The maximum magic power.</param>
* <param name="strength"> The strength.</param>
* <param name="agility"> The agility.</param>
* <param name="attack_power"> The attack power.</param>
* <param name="defense_power">The defense power.</param>
* <param name="gold"> The gold carried by the character.</param>
* <param name="experience"> The experience the character is worth.</param>
* <param name="stop_resist"> …Run Code Online (Sandbox Code Playgroud) 更新:
在采用C++ 17时<filesystem>,语言中包含一个标题,它正是这样做的.请参阅编译器的文档以了解它是否受支持.
原始问题:
这是我的一段时间的好奇心:如何在不使用boost或任何第三方库的情况下遍历目录树?只是简单的'C++'(例如98,99,01,0x和1x规格都可以.)?它是在加强存在的前一天完成的,因此必须有一种方法来做到这一点.
我有一个项目的构建配置,我想一步到位运行.目前我必须手动构建每个配置(调试,配置文件,发布配置),它变得很烦人.我可以使用命令行功能吗?这将使它"变得更容易",因为我可以告诉脚本构建每个配置而不管启动的那个(我已经在为libs和头文件的构建结束分发这样做了).
visual-studio-2010 visual-studio visual-studio-2012 visual-studio-2013 visual-studio-2015
c++ ×9
auto-ptr ×1
compiler-bug ×1
const-cast ×1
constructor ×1
exception ×1
parameters ×1
pointers ×1
reference ×1
shared-ptr ×1
static-cast ×1
std-pair ×1
stdtuple ×1
templates ×1
throw ×1
tuples ×1
unique-ptr ×1
visual-c++ ×1
weak-ptr ×1