小编Cas*_*sey的帖子

只有两个成员的std :: pair和std :: tuple之间的区别?

a std::pairstd::tuple只有两个成员之间有区别吗?(除了明显std::pair需要两个,只有两个成员,tuple可能会少...)

c++ tuples visual-studio-2010 stdtuple std-pair

75
推荐指数
4
解决办法
4万
查看次数

无法将'this'指针从'const Line'转换为'Line&'解释?

这个方法:

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.

c++ compiler-errors

44
推荐指数
2
解决办法
5万
查看次数

Visual Studio 2012不同的值释放/调试模式

在调试和发布模式之间切换时,此代码在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

33
推荐指数
3
解决办法
1869
查看次数

C++模板专门化,可以明确地调用可以作为指针或引用的类型的方法

摘要

有没有办法在模板化类型上调用类方法,该模板可能是指针或引用而不知道哪个而不是获取编译器/链接器错误?


细节

我有一个模板化的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)

c++ templates pointers reference template-specialization

30
推荐指数
1
解决办法
1万
查看次数

C++:抛出异常,使用"新"或不使用?

是否适合使用throw new FoobarException(Baz argument);throw FoobarException(Baz argument);

在捕捉时我总是使用catch(FoobarException& e)"以防万一",但我无法找到一个可靠的答案,我是否必须在C++中使用新的或不使用(肯定是Java),或者它只是程序员的偏好.

c++ exception throw

26
推荐指数
2
解决办法
7263
查看次数

std :: unique_ptr vs std :: shared_ptr vs std :: weak_ptr vs std :: auto_ptr vs raw pointer

与使用原始指针的类似(但不限于)某些高级技术相比,每个智能指针的等效用途是什么?

我的理解很少,但我可以收集到:

  • 原始指针:只有当你真的,真的,真的,真的,知道你正在做什么并且在界面后面仔细隐藏用法时才使用.
  • std :: auto_ptr:过时从不使用.
  • std :: unique_ptr:在赋值时转移所有权的Singleton指针.
  • std :: shared_ptr:引用计数指针,该指针在赋值时不转移所有权,但会增加其引用计数.当所有引用都离开范围或明确地调用std::shared_ptr::reset底层时deallocator.
  • std :: weak_ptr: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指出智能指针应该被用作简单的抽象来处理资源管理; 那些在声明点无法在堆上创建但必须在以后创建的文件范围对象呢?

c++ auto-ptr shared-ptr weak-ptr unique-ptr

18
推荐指数
1
解决办法
9855
查看次数

添加const-ness与static_cast和const_cast的"this"对象之间的C++区别?

根据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.

c++ const-cast static-cast

14
推荐指数
2
解决办法
8128
查看次数

改进了13参数构造函数

感谢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++ parameters constructor

11
推荐指数
1
解决办法
561
查看次数

在C++中遍历目录树

更新: 在采用C++ 17时<filesystem>,语言中包含一个标题,它正是这样做的.请参阅编译器的文档以了解它是否受支持.

原始问题:

这是我的一段时间的好奇心:如何在不使用boost或任何第三方库的情况下遍历目录树?只是简单的'C++'(例如98,99,01,0x和1x规格都可以.)?它是在加强存在的前一天完成的,因此必须有一种方法来做到这一点.

c++

9
推荐指数
2
解决办法
3031
查看次数

Visual Studio:一步构建多个配置?

我有一个项目的构建配置,我想一步到位运行.目前我必须手动构建每个配置(调试,配置文件,发布配置),它变得很烦人.我可以使用命令行功能吗?这将使它"变得更容易",因为我可以告诉脚本构建每个配置而不管启动的那个(我已经在为libs和头文件的构建结束分发这样做了).

visual-studio-2010 visual-studio visual-studio-2012 visual-studio-2013 visual-studio-2015

9
推荐指数
1
解决办法
1835
查看次数