C++ 如何立即更改项目中所有引用的变量名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11337294/
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 15:02:40  来源:igfitidea点击:

How to instantly change name of variable for all references in project

c++visual-studio-2010visual-studiorefactoring

提问by Takarakaka

I am working on some game in VS10, and I want to change the name of some variable, because I want to expand the game, however there is so much references to it, that I don't know from where to start. Is there any hotkey in VS10 that will do this in a moment for me? I know there is NetBeans hotkey CTRL+H, but this doesn't work for VS10.

我正在 VS10 中开发一些游戏,我想更改某个变量的名称,因为我想扩展游戏,但是对它的引用太多了,我不知道从哪里开始。VS10 中是否有任何热键可以立即为我执行此操作?我知道有 NetBeans 热键 CTRL+H,但这不适用于 VS10。

回答by Old Geezer

First, any variable should always be declared and used in the smallest of scope.

首先,任何变量都应始终在最小范围内声明和使用。

In VS2010, whenever you rename a variable, it will prompt you whether you want all references to this variable is to be so renamed. A little red bar will appear underneath the name where you changed it. Move your mouse to that bar and an icon will appear with "Options to update references to the renamed object". You can choose between renaming all right away or with preview.

在VS2010中,每当你重命名一个变量时,它会提示你是否希望对这个变量的所有引用都如此重命名。在您更改名称的下方会出现一个红色的小条。将鼠标移到该栏,将出现一个带有“更新对重命名对象的引用的选项”的图标。您可以选择立即重命名或预览。

(The ability to rename all references to the renamed variable disappears when you go and rename another different variable. Perhaps there is a way to go back to an older rename but I have not found it.)

(当你去重命名另一个不同的变量时,重命名对重命名变量的所有引用的能力就会消失。也许有一种方法可以回到旧的重命名,但我还没有找到。)

I tried renaming a public property of a class. The automatic renaming works too for references to that public property, at least when both are within the same code file.

我尝试重命名类的公共属性。自动重命名也适用于对该公共属性的引用,至少当两者都在同一个代码文件中时。

回答by Salami

As Retired Ninja said in his comment, you pretty much need add-ons for Visual Studio (like Visual Assist X).

正如 Retired Ninja 在他的评论中所说,您几乎需要 Visual Studio 的附加组件(如 Visual Assist X)。

Installing Visual Assist will give you a menu like this:

安装 Visual Assist 将为您提供如下菜单:

enter image description here

在此处输入图片说明

... where the "Find References" button searches for all references to that variable/function/namespace/etc and replaces it (not just a plain text search).

...其中“查找引用”按钮搜索对该变量/函数/命名空间/等的所有引用并替换它(不仅仅是纯文本搜索)。

Other alternative is to open the "Find and Replace" window (press Ctrl+F), and click "Quick Replace". From there you can do a plain text search and replace (there's also wildcard and regex support (under "Find Options->Use:), if that helps).

其他替代方法是打开“查找和替换”窗口(按 Ctrl+F),然后单击“快速替换”。从那里您可以进行纯文本搜索和替换(还有通配符和正则表达式支持(在“查找选项->使用:)下,如果有帮助的话)。

回答by Mathieu K.

There is CTRL+H in VS10 too. If you press it on the right side pops out panel which is basically interface for what you want to do about the modification. It has everything included, to modify that name only in the document/s which is/are currently opened, or even in the whole project. Bad thing is that replaces that name with everything it encounters, except for header name. So if I have something like fruit.h and I want to rename it in apple.h, everything will be renamed to apple.h except the name file, so it will throw an exception that aformentioned header couldn't be found.

VS10 中也有 CTRL+H。如果您在右侧按下它,则会弹出面板,该面板基本上是您想要进行修改的界面。它包含所有内容,仅在当前打开的文档或什至整个项目中修改该名称。不好的是用它遇到的所有内容替换该名称,但标题名称除外。所以如果我有像fruit.h这样的东西并且我想在apple.h中重命名它,除了名称文件之外,所有东西都将重命名为apple.h,所以它会抛出一个异常,无法找到上述头文件。