Writing and refactoring code (C++)

The Visual C++ code editor and IDE provide many coding aids. Some are unique to C++, and some are essentially the same for all Visual Studio languages. For more information about the shared features, see Writing Code in the Code and Text Editor. Options for enabling and configuring C++-specific features are located under Tools | Options | Text Editor | C/C++. After choosing which option you want to set, you can get more help by pressing F1 when the dialog is in focus. For general code formatting options, type Editor C++ into QuickLaunch.

Experimental features, which may or may not be included in a future version of Visual Studio, are found in the Text Editor C++ Experimental dialog. In Visual Studio 2017 you can enable Predictive IntelliSense in this dialog.

Adding new files

To add new files to a project, right-click on the project node in Solution Explorer and choose Add | New.

Formatting options

To set formatting options such as indents, brace completion, and colorization, type "C++ Formatting" into the QuickLaunch window. Visual Studio 2017 version 15.7 and later supports ClangFormat. You can configure it in the C/C++ Formatting Property Page under Tools | Options | Text Editor | C/C++ | Formatting.

C++ formatting options

IntelliSense

IntelliSense is the name for a set of features that provide inline information about members, types, and function overloads. The following illustration shows the member list drop-down that appears as you type. You can press the tab key to enter the selected item text into your code file.

C++ member list drop down

For complete information see Visual C++ IntelliSense.

Insert Snippets

A snippet is a predefined piece of source code. Right-click on a single point or on selected text to either insert a snippet or surround the selected text with the snippet. The following illustration shows the three steps to surround a selected statement with a for loop. The yellow highlights in the final image are editable fields that you access with the tab key. For more information, see Code Snippets.

Visual C++ Insert Snippet Drop-down

Add Class

Add a new class from the Project menu by using the Class Wizard.

Add New Class in Visual C++

You can also use Class Wizard to modify or examine an existing class.

Visual C++ Class Wizard

For more information, see Adding Functionality with Code Wizards (C++).

Refactoring

Refactorings are available under the Quick Action context menu, or by clicking on a light bulb in the editor. Some are also found in the Edit > Refactor menu. These features include:

Visual C++ shares many code navigation features with other languages. For more information, see Navigating Code and Viewing the Structure of Code.

QuickInfo

Hover over a variable to see its type information.

Visual C++ QuickInfo

Open document (Navigate to header)

Right click on the header name in an #include directive and open the header file.

Visual C++ Open Document menu option

Peek Definition

Hover over a variable or function declaration, right-click, then choose Peek Definition to see an inline view of its definition. For more information, see Peek Definition (Alt+F12).

Visual C++ Peek Definition

Go To Definition

Hover over a variable or function declaration, right-click, then choose Go To Definition to open the document where the object is defined.

View Call Hierarchy

Right click on any function call and view a resursive list of all the functions that it calls, and all the functions that call it. Each function in the list can be expanded in the same way. For more information, see Call Hierarchy.

Visual C++ Call Hierarchy

Toggle Header / Code File

Right-click and choose Toggle Header / Code File to switch back and forth between a header file and its associated code file.

Outlining

Right-click anywhere in a source code file and choose Outlining to collapse or expand definitions and/or custom regions to make it easier to browse only the parts you are interested in. For more information, see Outlining.

Visual C++ Outlining

Scrollbar map mode

Scrollbar map mode enables you to quickly scroll and browse through a code file without actually leaving your current location. Or click anywhere on the code map to go directly to that location. For more information, see How to: Track your code by customizing the scrollbar.

Code Map in Visual C++

Generate graph of include files

Right click on a code file in your project and choose Generate graph of include files to see a graph of which files are included by other files.

Visual C++ graph of include files

F1 Help

Place the cursor on or just after any type, keyword or function and press F1 to go directly to the relevant reference topic on docs.microsoft.com. F1 also works on items in the error list, and in many dialog boxes.

Quick Launch

To easily navigate to any window or tool in Visual Studio, simply type its name in the Quick Launch window in the upper right corner of the UI. The auto-completion list will filter as you type.

Visual Studio Quick Launch