Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Book Excerpt: Essential Guide to Managed Extensions in C++

Managed Extensions for C++ are added to the Visual C++ .NET compiler to allow access to .NET Framework functionality. Visual C++ .NET is the only .NET language that can generate both native and managed code, and its rich set of features let you write managed applications for the .NET platform. This comprehensive guide to coding managed extensions for C++ was written by key members of the Visual C++ .NET compiler development teampeople who have spent most of their time implementing the language and educating others about managed C++. Read Chapter 9, ''Properties.''




Application Security Testing: An Integral Part of DevOps

hapter 9: Properties
You have heard about data encapsulation. Roughly speaking, what it means is that all data members should be hidden behind public interfaces. Quite often you might find yourself writing code like this:

class Student { int age_; //... public: //... int GetAge(){return age_;} void SetAge(int age){age_=age;} };

And then you might use class Student in the following manner:

int CompareByAge(Student*student1,Student*student2 ) { return student1->GetAge()-student2->GetAge(); }

What's wrong with this code? The data encapsulation principle is obeyed—but at the price of code readability.

And this is exactly when properties are useful. A convenient syntactic notation long enjoyed by Visual Basic programmers, properties have actually been around in Visual C++ for quite some time supported by __declspec(property). However, the __declspec(property)syntax wasn't very popular with C++ programmers.

Fortunately, properties have made their way into the .NET family of languages, including MC++. This chapter shows how to use scalar and indexed properties, explains what you can and cannot do with properties, and demonstrates how properties work.

Managed Properties: First Steps
Properties supported in MC++ are called managed properties. A property is declared using the __property keyword followed by a property method declaration.

Note that the name of property methods must start with the prefix get_ or set_. The method with the get_prefix is called a getter and the method with the set_prefix is called a setter. The name of the getter must be the same as the name of the setter except for the prefix. Here is how you would rewrite the preceding example using managed properties:

#using<mscorlib.dll> __gc class Student { int age_; //... public: //... __property int get_Age(){return age_;} __property void set_Age(int age){age_=age;} };

Using properties is now just as simple as this:

int CompareByAge(Student*student1,Student*student2) { return student1->Age -student2->Age; }

As you can see, a property can be used like a data member. In this example, the compiler replaces Age with a call to the appropriate method, which in this con-text is get_Age.

Note the keyword __gc in front of the class declaration. Managed properties can only be declared in managed classes—that is, either gc classes or value types.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date