如何在Visual Studio中基于字符串比较设置条件断点?

30

这是我多年来一直尝试但从未完全成功的内容。我只想为基于字符串相等性的 Visual C++ 2012 设置一个条件断点。我要测试的变量是

string test;

我尝试过

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.
2个回答

6

针对在Visual Studio中使用,这个问题已经在这里得到了解答。特别是,OBWANDO的回答中提供的字符串可以用于设置断点条件。但请注意,它有点笨拙。当断点被触发时,您将收到一个警告消息,尽管调试器已经停止。但它似乎不会造成任何损害。


是的,那篇帖子回答了我的问题,OBWANDO的解决方案对我也非常有效。感谢您指出这一点。 - Kit Fisto

-5
您可以使用以下便携且简单的方法:
if (!test.compare("foo")) {
    int dummy = 0; // any statement, put breakpoint here
}

1
谢谢,但我正在寻找一种在不修改源代码的情况下设置断点条件的方法。 - Kit Fisto
你可以尝试使用 test[0] == ''。 - Richa Aggarwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接