C4Swimmers Newsletter  

(Tutorial) C++ Implementation of the C# Property and Indexer with Accessor-Modifiers

Tutorial: C++ Implementation of the C# Property and Indexer with Accessor-Modifiers 

A C++ implementation of the C# functionality for both Property and Indexer, and controlling their compile-time accessibility using accessor-modifiers.

Introduction

What this article presents is implementing in C++ the C# functionality for both Property and Indexer and controlling their compile-time accessibility using accessor-modifiers.

The provided source code contains a generic framework for performing both Property and Indexer in C++. The only thing that the user of this framework needs to implement is an Accessor functor, which requires set and get accessor functions. The framework provides pre-defined properties, each with different accessibility options, which will wrap around the user-defined Accessor functor. Thereby, to declare a property within a Host class is simply choosing a property based upon expected accessibility and assigning it with an Accessor.

Properties are known as smart fields, and enable access to member variables of a class, but maintains encapsulation by implicit access to its set and get accessors.

Indexers are also called smart arrays in C#, and can be used to use an object as an array. Similar to C# Properties, the subscript ([]) operator has access to its own set and get accessors.

Background

My programming lingua franca has been C++ for quite a while, and thereby it is my foundation for other programming languages like Java and C#. Every time I see some feature with these other languages, I am curious enough to query if that feature could be implemented in C++. C# Properties and Indexers is a case in point.

In CodeProject, I have read several articles on C++ implementation of C# Property, and none pertaining to C# Indexer. However, I did not find an article that provided compile-time check of declared accessibility of a Property's field.

The code presented in this article provides Property and Indexer functionality in C++, including compile-time check of declared accessibility of the assigned accessor-modifier for set and get accessors.

| Read More..

Courtesy: Codeproject.com