Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ Timesaving Techniques (2005) [eng].pdf
Скачиваний:
65
Добавлен:
16.08.2013
Размер:
8.35 Mб
Скачать

Implementing Properties 137

The problem is, you can also set age to an invalid value. The restriction is only implemented by the “rule” that an age can’t be outside the valid range of 18 to 80. Our code does not enforce this rule, which could easily cause problems in calculations that rely on the rule being obeyed. An invalid assignment might look like this:

f.age = 10; // Invalid

be useful for values that the programmer needs access to, but cannot possibly modify, such as the total memory available in the system. Properties like these save time by making the code easier to read while still maintaining data integrity.

Implementing Properties

The ideal solution would be to allow people to directly set the age property in this class, but not allow them to set it to values outside the valid range. For example, if a user did so and then added this statement

f.age = 10;

the age would not be set and would retain its old value. This resistance to unauthorized change is the advantage of a property, instead of allowing the value to change no matter what input value is given. In addition, we can create read-only properties that can be read but not written to. C++ does not offer this capability directly, but it allows us to create such a thing ourselves. A read-only property would

Creating a simple class that implements properties for a specific type — in this case, integers — can illustrate this C++ capability. We can then customize the class to allow only specific types of integers, or integer values.

1. In the code editor of your choice, create a new file to hold the code for the implementation of the source file.

In this example, the file is named ch25.cpp, although you can use whatever you choose.

2. Type the code from Listing 25-1 into your file.

Or better yet, copy the code from the source file on this book’s companion Web site.

LISTING 25-1: PROPERTY CLASS

#include <stdio.h> #include <string>

class IntProperty

{

int temp; int &iValue; bool bWrite;

public:

void Init()

{

bWrite = false;

}

IntProperty(void) : iValue(temp)

{

Init();

}

(continued)

138 Technique 25: Implementing Properties

LISTING 25-1 (continued)

virtual void set(int i)

{

iValue = i;

}

virtual int get(void)

{

return iValue;

}

public: IntProperty(int& i)

: iValue(i)

{

Init();

}

IntProperty( int i, bool read, bool write ) : iValue(i)

{

Init();

}

IntProperty( const IntProperty& aCopy ) : iValue(aCopy.iValue)

{

Init();

}

virtual ~IntProperty()

{

}

// Accessors

int getValue( void )

{

return iValue;

}

bool getWrite(void)

{

return bWrite;

}

void setWrite(bool write)

{

bWrite=write;

}

// Operators

IntProperty& operator=( int i )

{

if( bWrite ) set(i);

1

2

Implementing Properties 139

else

printf(“Trying to assign to a read-only property\n”);

return *this;

}

// Cast to int operator int()

{

return get();

}

};

This class implements a property according to the C++ standards, and yet works as if it were a Java or C# property. We will be able to read and write to the data value without having to write extra code, but the data values will be validated for the range allowed. To use it, we need to embed it in a class that the end user will interact with. This class will expose the IntProperty object to the end user, but the instance of the IntProperty class within any other class will work with an internal variable of that class. The IntProperty class is really just a wrapper around a reference to a variable, but that variable will be outside the scope of the

IntProperty class.

Notice that the set ( 1) and get ( 2) meth-

ods of the class are internal

to the class itself,

but are also declared as virtual. That means implementing a derived class that screens out certain data values would be trivial, as we will see in the AgeProperty class later in this technique.

To derive a class from our IntProperty class, we just have to override the get and set methods in the ways we want. To restrict the range of the integer value, for example, we modify the set method to only allow the values we permit. In addition, we must override the operator= method, because operator= is never inherited by a derived class. That’s because you could be setting only a portion of the object — which the language won’t let you do, so you have to override the operator as well. When you create a

derived class, the operator= would be called for the base class. This would set only the member variables in the base class, and would not set the ones in the derived class. Otherwise, the remainder of the class remains the same.

3. Add the code from Listing 25-2 to your source file.

We could simply create a new file to store this new class, but it is easier to just combine them for the purpose of this technique.

In this case, we are going to use the IntProperty in another class.

LISTING 25-2: EXTENDING THE INTPROPERTY CLASS

class AgeProperty : public IntProperty

{

private:

virtual void set(int i)

{

if ( i >= 18 && i <= 80 ) IntProperty::set(i);

}

public:

AgeProperty( int &var ) : IntProperty(var)

{

}

AgeProperty& operator=( int i )

{

IntProperty::operator=(i); return *this;

}

};

140 Technique 25: Implementing Properties

Now, in order to use the class, we need to embed the object as a public member of our encapsulating class, and provide it with a data member that it can access to set and get values. The property class is just a wrapper around a value. Because it contains a reference to a data value outside the class, it can directly modify data in another class. That means that any changes made to the reference in the IntProperty class will be immediately reflected in the underlying class-member variable.

To show how it all fits together, the next section adds a class that makes use of the AgeProperty class.

Testing the Property Class

After we have defined the Property class, we need to test it. The following steps show you how:

1. In the code editor of your choice, create a new file to hold the code for your test program.

In this example, I named the test program ch25.cpp.

2. Put the code from Listing 25-3 into your testdriver file.

LISTING 25-3: TESTING THE INTVALUE CLASS

class TestIntValue

{

private:

int myInt; public:

TestIntValue() : i(myInt)

{

myInt = 0;

}

void Print()

{

printf(“myInt = %d\n”, myInt );

}

public: AgeProperty i;

};

This class contains a single integer value, its only data member. The data member is associated with the Property class in the constructor for the class, so any changes to the member variable will be immediately reflected in the Property class and the TestIntValue class at the same time.

Because the data value is used by reference in the Property class, changing the property is the equivalent of changing the original data member directly. We are controlling how the data is changed, while allowing the compiler to generate the code that does the actual data manipulation.

Our class illustrates how the data values change and what they are assigned to at any given moment in time. We will use this class to show off how the property class works.

3. Add the code from Listing 25-4 to the end of your existing file.

LISTING 25-4: THE TEST DRIVER CODE

int main(int argc, char **argv)

{

TestIntValue tiv;

tiv.i

= 23;

 

3

printf(“Value = %d\n”, (int)tiv.i );

 

tiv.Print();

 

5

tiv.i.setWrite( true );

 

tiv.i

= 23;

 

printf(“Value = %d\n”, (int)tiv.i );

 

 

int x

= tiv.i;

 

 

tiv.Print();

 

 

printf(“X = %d\n”, x );

 

 

tiv.i

= 99;

 

 

printf(“Value = %d\n”, (int)tiv.i );

}

4. Save the source file in your code editor and close the editor application.

5. Compile the file with your favorite compiler on your favorite operating system.

If you have done everything properly, you should see the following output from the application:

Testing the Property Class 141

$ ./a.exe

 

 

Trying to assign to a read-only property

4

Value = 0

 

myInt = 0

6

Value = 23

 

myInt = 23

 

X = 23

 

 

Value = 23

 

 

The output above illustrates that our property class is working properly. The initial value of the integer is 0, as specified in the constructor. Because the class defaulted to read-only (setWrite was not yet called), an attempt to write to the variable ( 3) results in no change being made ( 4). After we set the write

flag to allow changes ( 5), we can then assign values to the variable and have it modified in the output ( 6).

Properties are an essential part of languages such as C# and Java, but are not yet a part of the C++ languages. If you get into the habit of thinking about them, however, you can save a lot of time in the long run — for one thing, you won’t have to relearn how to use data members for classes. Translating code to and from C++ from the newer languages will become an essential part of mixed language projects in the future, and making it easy to do that translation will save you a lot of time and effort.