- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
OBJECT SEMANTICS IN C++/CLI in Visual C#
CHAPTER 4 OBJECT SEMANTICS IN C++/CLI Paint PDF 417 In C# Using Barcode creation for .NET Control to generate, create PDF417 image in Visual Studio .NET applications. www.OnBarcode.comReading PDF417 In C# Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comFigure 4-3. Value types and reference types being passed to functions
Quick Response Code Generator In C# Using Barcode creation for .NET framework Control to generate, create QR image in .NET framework applications. www.OnBarcode.comEncoding Matrix 2D Barcode In Visual C# Using Barcode drawer for .NET Control to generate, create Matrix Barcode image in VS .NET applications. www.OnBarcode.comPassing Reference Types by Value
Code 128 Creation In Visual C# Using Barcode generator for .NET Control to generate, create USS Code 128 image in Visual Studio .NET applications. www.OnBarcode.comGenerating GS1 128 In Visual C#.NET Using Barcode creator for .NET Control to generate, create GTIN - 128 image in VS .NET applications. www.OnBarcode.comTo pass reference type objects by value, omit the handle symbol, as shown in the declaration of the function h in Listing 4-23. While this is normal for a value type, it is also possible for a reference type as long as the reference type has a copy constructor. Listing 4-23. Passing a Reference Type by Value // pass_by_value.cpp using namespace System; ref struct R { R() { val = 1; } // copy constructor R( R% r) { val = r.val; } property int val; }; Print Barcode In C# Using Barcode creation for VS .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comDUN - 14 Drawer In C#.NET Using Barcode creation for Visual Studio .NET Control to generate, create UPC Case Code image in .NET framework applications. www.OnBarcode.comCHAPTER 4 OBJECT SEMANTICS IN C++/CLI
Encoding PDF417 In Java Using Barcode generation for Java Control to generate, create PDF 417 image in Java applications. www.OnBarcode.comDrawing PDF-417 2d Barcode In None Using Barcode encoder for Font Control to generate, create PDF 417 image in Font applications. www.OnBarcode.com// R passed by value (no ^) void f(R r_local) { // Modify r without affecting outer scope. r_local.val = 2; Console::WriteLine("Within f: " + r_local.val); } int main() { R r; f(r); Console::WriteLine("Outside f: " + r.val); // The same code, using heap semantics R^ rhat = gcnew R(); f(*rhat); // Dereference the handle. Console::WriteLine("Outside f: " + rhat->val); } The output of Listing 4-23 is as follows: Within f: 2 Outside f: 1 Within f: 2 Outside f: 1 Figure 4-4 shows what s happening in memory for this type of parameter passing. Making ANSI/AIM Code 128 In .NET Using Barcode generator for Reporting Service Control to generate, create Code128 image in Reporting Service applications. www.OnBarcode.comEAN128 Generation In None Using Barcode maker for Online Control to generate, create GS1-128 image in Online applications. www.OnBarcode.comFigure 4-4. The main method declares R r and passes by value to f(R r_local). The copy constructor is invoked, creating another copy of the reference type object on the managed heap. Scanning European Article Number 13 In Visual C#.NET Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comScanning DataMatrix In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comCHAPTER 4 OBJECT SEMANTICS IN C++/CLI
PDF 417 Reader In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comEncode EAN / UCC - 13 In Java Using Barcode generator for Java Control to generate, create EAN13 image in Java applications. www.OnBarcode.comPassing Value Types by Reference
QR Maker In Java Using Barcode drawer for Java Control to generate, create QR-Code image in Java applications. www.OnBarcode.comPainting Code 128 Code Set A In Java Using Barcode printer for Java Control to generate, create Code 128 Code Set C image in Java applications. www.OnBarcode.comParameters normally passed by value (such as primitive types and value types) may be passed by reference using the % symbol in the parameter declaration. This is useful if you want to write a function that changes a value type, such as in Listing 4-24. Listing 4-24. Changing a Value Type in a Function // pass_by_ref.cpp using namespace System; value class Pair { public: int x; int y; }; void swap_values(Pair% pair) { int temp = pair.x; pair.x = pair.y; pair.y = temp; } int main() { Pair p; p.x = 5; p.y = 3; Console::WriteLine("{0} {1}", p.x, p.y); swap_values(p); Console::WriteLine("{0} {1}", p.x, p.y); } The output of Listing 4-24 is shown here: 5 3 3 5 Figure 4-5 shows the memory layout for the preceding example. Making EAN 128 In .NET Using Barcode creator for .NET framework Control to generate, create GS1 128 image in .NET framework applications. www.OnBarcode.comUPC Code Scanner In C# Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comCHAPTER 4 OBJECT SEMANTICS IN C++/CLI
Figure 4-5. The swap_values method contains a reference pair to main s copy of p as well as a local integer variable to hold a temporary value. Temporary Handles
In Listing 4-12, you saw how you could pass a handle by reference using the ^% indirection. In Listing 4-25, you see that a reference type object with stack semantics isn t affected by a function that takes a reference to a handle if you use the tracking reference operator (%) to create a handle to the object. This is because the handle obtained with the % operator is a different, temporary handle. As a result, an object with stack semantics always represents the original object, so you can be assured that the correct object is cleaned up at the end of the block. Listing 4-25. C++/CLI Stack Semantics // pass_by_ref2.cpp // This example illustrates that a stack semantics // reference type can't be redirected by a function // that operates on references to handles. using namespace System; ref struct R { property int A; R(int a) { this->A = a; } }; // Takes a reference to a handle void reset_handle(R^% r) { r = gcnew R(5); } int main() { R r(2); // stack semantics reset_handle(%r); // Use % to create a handle. // The output is 2, since the handle passed to f CHAPTER 4 OBJECT SEMANTICS IN C++/CLI
// was a temporary one, so it didn't get changed by // the function f. Console::WriteLine("Value: {0}", r.A); } It s worth having a solid understanding of the semantics of parameter passing. Using what you ve seen so far, see if you can predict the output of Listing 4-26. Listing 4-26. What Does This Output // pass_by_ref3.cpp // This example requires some careful thought. // Can you figure out what the final output // will be using namespace System; ref struct R { property int A; R(int a) { this->A = a; } }; // Takes a reference to a handle. This function // sets the property value on the object, then // overwrites the object! // Will the object in the calling scope // have the value 3, or 5, or will it retain its // original value void reset_handle(R^% r) { r->A = 3; r = gcnew R(5); } int main() { R r_stack(1); // stack semantics R^ r_heap = gcnew R(2); // heap semantics reset_handle(%r_stack); // Use % to create a handle. reset_handle(r_heap); Console::WriteLine("Final value, stack semantics: {0}", r_stack.A); Console::WriteLine("Final value, heap semantics: {0}", r_heap->A); }
|
|