-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_types.cpp
More file actions
65 lines (50 loc) · 1.62 KB
/
value_types.cpp
File metadata and controls
65 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// This is the main project file for VC++ application project
// generated using an Application Wizard.
//
// Written by Chris Maunder ([email protected])
// The Code Project, http://www.codeproject.com
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
// Declare a new value type (will be allocated on the stack)
__value struct Complex
{
double real;
double imaginary;
virtual String *ToString()
{
return String::Format("{0} + {1}i", real.ToString("N2"),
imaginary.ToString("N2"));
}
};
// This is the entry point for this application
#ifdef _UNICODE
int wmain(void)
#else
int main(void)
#endif
{
// declare and work with a value type
Console::WriteLine("Demonstration of a value type declarion and creation\n");
Complex z;
Console::WriteLine("z is initialized to {0}", z.ToString());
z.real = 1.0;
z.imaginary = -3.1415;
Console::WriteLine("z is now {0}", z.ToString());
// Boxing demonstration
Console::WriteLine("\nDemonstration of boxing\n");
Object* obj = __box(z.real);
Console::WriteLine("The boxed value of z.real is {0}", obj);
// Accessing data fields of a boxed structure
Console::WriteLine("\nDemonstration accessing data fields in a boxed type\n");
__box Complex* pZ = __box(z);
pZ->real = 4;
Console::WriteLine("The boxed value of pZ->real is {0}", pZ->real.ToString());
// Unboxing demonstration
Console::WriteLine("\nDemonstration of unboxing\n");
Complex w = * dynamic_cast<Complex*>(pZ);
Console::WriteLine("The unboxed value of w.real is {0}", w.real.ToString());
Console::Write("Press Enter to continue");
Console::ReadLine();
return 0;
}