< C++ .NET Value Type 1 | Main | C++ .NET Value Type 3 >


 

 

More on Value Types 2

 

 

What we have in this page?

 

  1. Debugging the Structure

  2. Differences Between Structures and Classes

  3. Implementing Constructors for a Struct

 

 

Debugging the Structure

 

In this exercise, you will run the program under control of the debugger so that you can look at the structure of the value type you have created.

 

1.   If you closed the Structs project, open it again (Open Project/Solution xxxx.sln) and open the source file. Insert a debug breakpoint by clicking in the gray border to the left of the code by clicking next to the declaration of p1. A red dot appears in the border.

 

Setting a breakpoint for code debugging

 

2.   Start the debugging session by pressing F5. Once the program has loaded, it will execute and stop at the breakpoint. You can now use the Locals window at the bottom of the screen to look at the structure of the Point type.

3.  If the Locals window isn’t displayed in debug mode, display it by pressing Ctrl+Alt+V followed by L. You should see an entry for the variable p1. Any type that has internal structure such as Point will have a plus sign (+) to the left of the variable name.

4.   Click on the plus sign to expand the structure. You’ll see a display similar to the one shown in the following figure.

 

Locals windows information during the debug process

 

You can see that p1 has three entries under it. The first shows that it’s derived from System::ValueType, which is in turn derived from System::Object. The other two are the x and y members, which are both 32-bit integers. At this point in time, they haven’t been given a useful initial value.

 

5.   Press F10 (Step Over) three times to initialize p1 and execute the next two assignment statements. This action will result in p1 being initialized, and you will see the values of x and y change to 10 and 20, respectively. The values also change from black to red in the Locals window, showing that they were changed in the previous execution step.

 

Local windows information update during the debug process

 

6.  Continue pressing F10 to single-step through the code, examining the changes that occur to p1. When you’re done, click the Stop Debugging button on the toolbar (a blue square) or press Shift+F5.

 

Differences Between Structures and Classes

 

Structures and classes have several fundamental differences:

  1. You can’t initialize members in a structure definition. If you need to provide initialization for a structure type, you have to provide a constructor.

  2. Structures can’t have finalizers because they aren’t garbage collected.

  3. Inheritance isn’t applicable to structs, so they can’t inherit from anything else and can’t be used as a base class.

  4. Structs can implement interfaces.

Implementing Constructors for a Struct

 

In the next exercise, you will add a constructor to the Point struct so that instances can be initialized on creation.

 

1.        Continue using the project from the previous exercise.

2.        Add the following line immediately after the public declaration in your Point structure definition:

Point(int xVal, int yVal) { x = xVal; y = yVal; }

Defining a constructor for a structure

 

A constructor that takes no arguments is called a default constructor shown below.

Point();

For the managed type, the default constructor is set by the compiler so no need to provide the default constructor. In this code, the constructor takes two int values and uses them to initialize the x and y data members. In this case, the arguments are simply being copied into the data members, but it would be simple to add some checking to ensure that the data passed in is correct. Anyone who has used C++ before will be familiar with the use of default arguments on constructors. You can’t use default arguments on managed types in Visual C++, so you need to provide an explicit default constructor.

 

3.        You can now edit and add extra code to your main() function to create initialized Points shown below.

 

      // Create a Point

      Point p1;  // use the default constructor

      Point p2(10,20);   // use the second constructor to set x to 10 and y to 20

      // Initialize its members

      p1.x = 10;

      p1.y = 20;

      // Write to the standard output

      Console::Write("p1.x is ");

      Console::WriteLine(p1.x);

      Console::Write("p2.x is ");

      Console::WriteLine(p2.x);

      Console::Write("p2.y is ");

      Console::WriteLine(p2.y);

 

 

 

 

 

 

 

 

 

---------------------------------------------------------------

Creating and initializing a structure object

 

4.        Compile and run your program. The output should be something shown below.

 

Structure with constructor console program output sample

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4

 


 

< C++ .NET Value Type 1 | Main | C++ .NET Value Type 3 >