C++ Language (How Do I in Visual C+)
This page links to help on tasks related to the Visual C++ language using Visual C++. To view other categories of popular tasks covered in Help, see How Do I in Visual C++.
Differences from Managed Extensions for C++
Managed Extensions for C++ Syntax Upgrade Checklist
Lists the syntactic differences between Managed Extensions for C++ and the new C++ syntax.C++/CLI Migration Primer
A detailed guide to migrating code from Managed Extensions for C++ to Visual C++ 2008.
Visual C++ Examples
Arrays
How to: Create Single-Dimension Arrays
Shows how to create single-dimension arrays of reference, value, and native pointer types.How to: Create Multidimension Arrays
Shows how to create multi-dimension arrays of reference, value, and native pointer types.How to: Iterate Over Arrays with for each
Shows how to use the for each, in keyword on different types of arrays.How to: Create Arrays of Managed Arrays (Jagged Arrays)
Shows how to create single-dimension arrays of managed array of reference, value, and native pointer types.How to: Sort Arrays
Demonstrates how to use the Sort method to sort the elements of an array.How to: Sort Arrays Using Custom Criteria
Demonstrates how to sort arrays by implementing the IComparable interface.How to: Make Typedefs for Managed Arrays
Shows how to make a typedef for a managed array.... (Variable Argument Lists)
Shows how functions with a variable number of arguments can be implemented in Visual C++ using the ... syntax.How to: Use Managed Arrays as Template Type Parameters
Shows how to use a managed array as a parameter to a template.How to: Declare and Use Interior Pointers and Managed Arrays
Shows how you can declare and use an interior pointer to an array.
Boxing and Casting
How to: Use gcnew to Create Value Types and Use Implicit Boxing
Shows how to use gcnew on a value type to create a boxed value type, which can then be placed on the managed, garbage-collected heap.How to: Unbox
Shows how to unbox and modify a value.How to: Explicitly Request Boxing
Shows how to explicitly request boxing by assigning a variable to a variable of type Object.How to: Downcast with safe_cast
Shows how to downcast from a base class to a class derived from the base class using safe_cast.How to: Use safe_cast and Boxing
Shows how to use safe_cast to box a value on the CLR heap.How to: Use safe_cast and Generic Types
Shows how to use safe_cast to perform a downcast with a generic type.How to: Use safe_cast and Unboxing
Shows how to use safe_cast to unbox a value on the CLR heap.How to: Use safe_cast and User-Defined Conversions
Shows how to invoke user-defined conversions by using safe_cast.How to: Upcast with safe_cast
Shows how to do an upcast—a cast from a derived type to one of its base classes—using safe_cast.
Data Types and Interfaces
How to: Instantiate Classes and Structs
Demonstrates that reference types and value types can only be instantiated on the managed heap, not on the stack or on the native heap.How to: Convert with Constructors
Introduces converting constructors, constructors that take a type and use it to create an object.How to: Define an Interface Static Constructor
Introduces static constructors, constructors which can be used to initialize static data members.How to: Define Static Constructors in a Class or Struct
Demonstrates how to create a static constructor.How to: Write Template Functions that Take Native, Value, or Reference Parameters
Demonstrates that by using a tracking reference in the signature of a template function, you can ensure that the function can be called with parameters whose type are native, CLR value, or CLR reference.
Enumerations
How to: Specify Underlying Types of Enums
Shows how to specify the underlying type of an enum.How to: Convert Between Managed and Standard Enumerations
Demonstrates how to convert between an enum and an integral type by using a cast.
Events and Delegates
How to: Compose Delegates
Demonstrates how to compose delegates.How to: Define and Use Delegates
Demonstrates how to define and use a delegate.How to: Define and Use Static Events
Shows how to define and use static events.How to: Define Event Accessor Methods
Shows how you can define an event's behavior when handlers are added or removed, and for when an event is raised.How to: Implement Abstract Events
Shows how to implement an abstract event.How to: Implement Managed Virtual Events
Shows how to implement virtual, managed events in an interface and class.How to: Access Events in Interfaces
Shows how to access an event in an interface.How to: Add Multiple Handlers to Events
Demonstrates that an event receiver, or any other client code, can add one or more handlers to an event.How to: Associate Delegates to Members of a Value Class
Shows how to associate a delegate with a member of a value class.How to: Associate Delegates to Unmanaged Functions
Shows how to associate a delegate with a native function by wrapping the native function in a managed type, and declaring the function to be invoked through P/Invoke.How to: Override Default Access of add, remove, and raise Methods
Shows how to override the default access on the add, remove, and raise events methods.How to: Raise Events Defined in a Different Assembly
Shows how to consume an event and event handler defined in one assembly by another assembly.
Exceptions
Basic Concepts in Using Managed Exceptions
Discusses the basic concepts for exception handling in managed applications.Differences in Exception Handling Behavior Under /CLR
Discusses differences from the standard behavior of exception handling and some restrictions in detail.How to: Define and Install a Global Exception Handler
Demonstrates how unhandled exceptions can be captured.How to: Catch Exceptions in Native Code Thrown from MSIL
Shows how to catch CLR exceptions in native code with __try and __except.finally
Discusses the CLR exception handling finally clause.Basic Concepts in Using Managed Exceptions
Discusses the basic concepts for exception handling in managed applications.
For Each
How to: Iterate Over Arrays with for each
Shows how to use the for each, in keyword on different types of arrays.How to: Iterate Over a Generic Collection with for each
Demonstrates how to create generic collections and iterate over them using for each, in.How to: Iterate Over a User-Defined Collection with for each
Demonstrates how to iterate over a user-defined collection using for each, in.How to: Iterate Over STL Collection with for each
Demonstrates how to iterate over STL collections using for each, in.
Generics
Overview of Generics in Visual C++
Provides an overview of generics, parameterized types supported by the Common Language Runtime.Generic Functions
Discusses generic functions, a function that is declared with type parameters.Generic Classes (Visual C+)
Describes how to create a generic class.Generic Interfaces (Visual C+)
Describes how to create a generic interface.Generic Delegates (Visual C+)
Describes how to create a generic delegate.Constraints
Describes that constraints are a requirement that types used as type arguments must satisfy.Consuming Generics from Other .NET Languages
Discusses how generics authored in one .NET language may be used in other .NET languages.Generics and Templates
Provides an overview of the many differences between generics and templates.How to: Convert Generic Classes
Shows how to convert a generic class to T.How to: Improve Performance with Generics
Discusses how generics can help increase the performance of an application that uses collections.
Pointers
How to: Declare Interior Pointers with the const Keyword
Shows how to use const in the declaration of an interior pointer.How to: Overload Functions with Interior Pointers and Native Pointers
Demonstrates that functions can be overloaded depending on whether the parameter type is an interior pointer or a native pointer.How to: Cannot Use Tracking References and Unary "Take-Address" Operator
Shows that a tracking reference cannot be used as a unary take-address operator.How to: Declare Pinning Pointers and Value Types
Shows that you can declare a pinning pointer to a value type object and use a pin_ptr to the boxed value type.How to: Declare Value Types with the interior_ptr Keyword
Demonstrates that an interior_ptr can be used with a value type.How to: Define the Scope of Pinning Pointers
Demonstrates that an object is pinned only while a pin_ptr points to it.How to: Pin Pointers and Arrays
Shows how to pin an array by declaring a pinning pointer to its element type, and pinning one of its elements.
Properties
How to: Use Simple Properties
Demonstrates that for simple properties—those that merely assign and retrieve a private data member—it is not necessary to explicitly define the get and set accessor functions.How to: Use Indexed Properties
Shows how to use default and user defined indexed properties.How to: Use Multidimensional Properties
Shows how to create multidimension properties that take a non-standard number of parameters.How to: Declare and Use Static Properties
Shows how to declare and use a static property.How to: Declare and Use Virtual Properties
Shows how to declare and use virtual properties.How to: Declare Abstract and Sealed Properties
Shows how to declare a sealed or abstract property by defining a non-trivial property and specifying the abstract or sealed keywords on the get and set accessor functions.How to: Overload Property Accessor Methods
Demonstrates how to overload indexed properties.
Tracking References
How to: Use Tracking References and Value Types
Shows simple boxing through a tracking reference to a value type.How to: Use Tracking References and Interior Pointers
Shows that taking the address of a tracking reference returns an interior_ptr and how to modify and access data through a tracking reference.How to: Pass CLR Types by Reference with Tracking References
Shows how to pass CLR types by reference with tracking references.
File Handling with Visual C++
How to: Read a Binary File (C++/CLI)
Demonstrates reading binary data from a file.How to: Write a Binary File (C++/CLI)
Demonstrates writing binary data to a file.How to: Read a Text File (C++/CLI)
Demonstrates how to open and read a text file one line at a time.How to: Write a Text File (C++/CLI)
Demonstrates how to create a text file and write text to it using the StreamWriter class.How to: Enumerate Files in a Directory (C++/CLI)
Demonstrates how to retrieve a list of the files in a directory.How to: Monitor File System Changes (C++/CLI)
Uses FileSystemWatcher to register for events corresponding to files being created, changed, deleted, or renamed.How to: Retrieve File Information (C++/CLI)
Demonstrates the FileInfo class. When you have the name of a file, you can use this class to retrieve information about the file such as the file size, directory, full name, and date and time of creation and of the last modification.
Windows Operations with Visual C++
How to: Write Data to the Windows Registry (C++/CLI)
Uses the CurrentUser key to create a writable instance of the RegistryKey class.How to: Read Data from the Windows Registry (C++/CLI)
Uses the CurrentUser key to read data from the Windows registry.How to: Retrieve Text from the Clipboard (C++/CLI)
Uses the GetDataObject member function to return a pointer to the IDataObject interface, which can then be queried for the format of the data and used to retrieve the actual data.How to: Store Text in the Clipboard (C++/CLI)
Uses the Clipboard object defined in the System.Windows.Forms namespace to store a string.How to: Retrieve the Windows Version (C++/CLI)
Demonstrates how to retrieve the platform and version information of the current operating system.How to: Read Windows Performance Counters (C++/CLI)
Shows how to retrieve and display a counter that is updated by Windows to indicate the percentage of time that the processor is busy.How to: Retrieve the Current Username (C++/CLI)
Shows how to retrieve the username of the currently logged in user.How to: Retrieve the Local Machine Name (C++/CLI)
Shows how to retrieve the local machine name.