C++ 如何在 Visual Studio 中根据字符串比较设置条件断点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21935563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:53:05  来源:igfitidea点击:

How to set conditional breakpoint based on string comparison in Visual Studio?

c++visual-studio-2012breakpointsstring-comparisonconditional-breakpoint

提问by Kit Fisto

This is something I have tried from time to time over the years and never quite succeeded. I just want to set a conditional break point for Visual C++ 2012 based on string equality. The variable I want to test is

这是我多年来不时尝试的方法,但从未成功过。我只想根据字符串相等性为 Visual C++ 2012 设置一个条件断点。我要测试的变量是

string test;

I tried

我试过

test == "foo"
=> The breakpoint cannot be set. no operator "==" matches these operands

test == string("foo")
=> The breakpoint cannot be set. no operator "==" matches these operands

test.compare("foo") == 0
=> The breakpoint cannot be set. This expression has side effects and will not be evaluated.

strcmp(test.c_str(), "foo") == 0
=> The breakpoint cannot be set. This expression has side effects and will not be evaluated.

采纳答案by rrirower

For use in Visual Studio, this has been answered here. In particular, the string provided in OBWANDO's answer can be used to set the break point condition. Note, however, that it is a bit klugy. You will receive a warning message when the breakpoint is hit even though the debugger has stopped. It doesn't appear to cause any harm.

对于在 Visual Studio 中使用,这已在此处回答。特别是OBWANDO的回答中提供的字符串可以用来设置断点条件。但是请注意,它有点笨拙。即使调试器已停止,当断点被击中时,您也会收到一条警告消息。它似乎不会造成任何伤害。

回答by Yury

You can use the following portable and simple way:

您可以使用以下便携且简单的方式:

if (!test.compare("foo")) {
    int dummy = 0; // any statement, put breakpoint here
}