Refactorings for C++
ReSharper provides the following refactorings for C++:
To perform a refactoring
- Set your caret at a symbol, select a code fragment that you want to refactor, or select an item in a tool window.
Do one of the following:
- On the main menu, choose , and then select a desired refactoring.
- In the editor, File Structure window, or other ReSharper window, right-click the item you want to transform, choose Refactor from the context menu, and then select the required refactoring.
- On the main menu, choose Refactor This in the context menu of a selection. , or press Ctrl+Shift+R to display the list of applicable refactorings, and select one of them. You can also choose
- If a refactoring could fix a code issue that ReSharper detects and highlights in the editor, the corresponding action indicator appears to the left of the code issue. You can click on this indicator or press Alt+Enter to check the suggested refactoring and apply if if necessary.
- Use default keyboard shortcuts assigned to specific refactorings, or assign custom shortcuts to your favorite refactorings.
If the selected refactoring requires user input,
the refactoring wizard opens. Note that the wizard's dialog boxes are not modal,
so you can edit the code while the wizard is open.
To roll back refactoring actions, the wizard provides the option To enable Undo, open all files with changes for editing. If you select this option, ReSharper opens all modified files in new editor tabs and enables you to roll the refactoring back. In this case, you will need to save the changes yourself. If this option is not selected, ReSharper saves modified files automatically, without opening them.
- If a refactoring operation would cause code conflicts (such as duplicate names, visibility conflicts, etc.), the wizard displays the list of conflicts on the last step, before you apply the refactoring. For some conflicts, the wizard can also suggest quick-fixes. For more information, see Resolving Conflicts in Refactorings.
Some refactorings are available immediately after you modify code in the editor. For more information, see Inplace Refactorings
The Change Signature refactoring allows you to make one or more different modifications to the signatures of a function. All usages, implementations, and overrides of the function will be updated accordingly.
You can also invoke this refactoring with the dedicated shortcut Ctrl+F6.
This refactoring allows you to create a new method based on a selected code fragment. ReSharper analyses the selected statements and detects variables that can be converted into method parameters or represent its return value.
You can also invoke this refactoring with the dedicated shortcut Ctrl+Alt+M.
Suppose that you want to extract the logic of calculating a discriminant to a separate method:
In the Extract Method dialog, you can pick method arguments, choose a return value and preview the resulted method:
As soon as you click Next, the new method is created and the selected expression is replaced with the method call.
This refactoring allows you to create a new field based on a selected expression, initialize it with the expression or from the constructor, and replace occurrences of the expression in the current type with references to the newly introduced field.
You can also invoke this refactoring with the dedicated shortcut Ctrl+Alt+D.
In the example below, we use this refactoring to replace two occurrences of the same string with a new private field and initialize it from the existing constructor:
|Before refactoring||After refactoring|
Introduce Namespace Alias
This refactoring helps you create a namespace alias for a namespace usage and replace the currently selected usage or all usages in the current document with the alias. Depending on the selected usages, the namespace alias is declared in closest possible scope to the usages.
To invoke this refactoring, set the caret at a namespace usage and press Ctrl+Shift+R or choose Introduce namespace alias in the Refactor This pop-up. If there are multiple occurrences of the namespace usage in the document, you will be able to choose whether to replace the current usage or all usages.in the menu, and then select
In the example below, we use this refactoring to add a namespace alias for the
|Before refactoring||After refactoring|
This refactoring allows you to create a new local variable based on a selected expression, initialize it with the expression, and finally replace all occurrences of the expression in the method with references to the newly introduced variable.
You can also invoke this refactoring with the dedicated shortcut Ctrl+Alt+V.
One of the most time-consuming refactorings is supported for C++. Modifying the name of a symbol can cause many problems, if you try to do it manually. When you invoke the Rename refactoring (also available with the dedicated F2 shortcut), all checks are done by ReSharper. Either all modification are performed smoothly, if no conflicts exist, or you get the list with conflicts that you can resolve manually to be sure that only necessary and appropriate changes will be made.