The discussion and the codes used supposed to be based on the new C++ .NET. The following are the topics in this part.
Nested Class Declarations
A class can be declared within the scope of another class. Such a class is called a "nested class". Nested classes are considered to be within the scope of the enclosing class and are available for use within that scope. To refer to a nested class from a scope other than its immediate enclosing scope, you must use a fully qualified name. The following example shows how to declare nested classes:
|
BufferedIO::BufferedInput and BufferedIO::BufferedOutput are declared within BufferedIO. These class names are not visible outside the scope of class BufferedIO. However, an object of type BufferedIO does not contain any objects of types BufferedInput or BufferedOutput. Nested classes can directly use names, type names, names of static members, and enumerators only from the enclosing class. To use names of other class members, you must use pointers, references, or object names. In the preceding BufferedIO example, the enumeration IOError can be accessed directly by member functions in the nested classes, BufferedIO::BufferedInput or BufferedIO::BufferedOutput, as shown in function good. Nested classes declare only types within class scope. They do not cause contained objects of the nested class to be created. The preceding example declares two nested classes but does not declare any objects of these class types.
friend Keyword
In some circumstances, it is more convenient to grant member-level access to functions that are not members of a class or to all functions in a separate class. The friend keyword allows a function or class to gain access to the private and protected members of a class. You can declare friend functions or friend classes to access not only public members but also protected and private class members.
friend class-name;
friend function-declarator;
A friend function is a function that is not a member of a class but has access to the class's private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges. Friends are not in the class's scope, and they are not called using the member-selection operators (. and –>) unless they are members of another class. A friend function is declared by the class that is granting access. The friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords. The following example shows a Point class and a friend function, ChangePrivate. The friend function has access to the private data member of the Point object it receives as a parameter.
// Variables.cpp : main project file.
// friend functions
// compile with: /clr
#include "stdafx.h"
using namespace System;
class Point
{
friend void ChangePrivate(Point &);
public:
Point( void ) : m_i(0) {}
void PrintPrivate( void ){Console::WriteLine("m_i = {0}", m_i); }
private:
int m_i;
};
void ChangePrivate(Point &i) {i.m_i++;}
int main()
{
Point sPoint;
sPoint.PrintPrivate();
ChangePrivate(sPoint);
sPoint.PrintPrivate();
return 0;
}
Output:
----------------------------------------------
Type Names in Class Scope
Type names defined within class scope are considered local to their class. They cannot be used outside that class. The following example demonstrates this concept:
// type names in class scope
// C2146 expected
class Tree
{
public:
typedef Tree * PTREE;
PTREE Left;
PTREE Right;
void *vData;
};
PTREE pTree; // not in class scope
Integer Limits
The limits for integer types are listed in the following table. These limits are also defined in the standard header file limits.h. It is Microsoft implementation.
Constant | Meaning | Value |
CHAR_BIT | Number of bits in the smallest variable that is not a bit field. | 8 |
SCHAR_MIN | Minimum value for a variable of type signed char. | –128 |
SCHAR_MAX | Maximum value for a variable of type signed char. | 127 |
UCHAR_MAX | Maximum value for a variable of type unsigned char. | 255 (0xff) |
CHAR_MIN | Minimum value for a variable of type char. | –128; 0 if /J option used |
CHAR_MAX | Maximum value for a variable of type char. | 127; 255 if /J option used |
MB_LEN_MAX | Maximum number of bytes in a multi character constant. | 5 |
SHRT_MIN | Minimum value for a variable of type short. | –32768 |
SHRT_MAX | Maximum value for a variable of type short. | 32767 |
USHRT_MAX | Maximum value for a variable of type unsigned short. | 65535 (0xffff) |
INT_MIN | Minimum value for a variable of type int. | –2147483648 |
INT_MAX | Maximum value for a variable of type int. | 2147483647 |
UINT_MAX | Maximum value for a variable of type unsigned int. | 4294967295 (0xffffffff) |
LONG_MIN | Minimum value for a variable of type long. | –2147483648 |
LONG_MAX | Maximum value for a variable of type long. | 2147483647 |
ULONG_MAX | Maximum value for a variable of type unsigned long. | 4294967295 (0xffffffff) |
_I64_MIN | Minimum value for a variable of type __int64 | -9223372036854775808 |
_I64_MAX | Maximum value for a variable of type __int64 | 9223372036854775807 |
_UI64_MAX | Maximum value for a variable of type unsigned __int64 | 18446744073709551615 (0xffffffffffffffff) |
Table 7 |
If a value exceeds the largest integer representation, the Microsoft compiler generates an error.
Floating Limits
The following table lists the limits on the values of floating-point constants. These limits are also defined in the standard header file float.h. It is Microsoft implementation.
Constant | Meaning | Value |
FLT_DIG DBL_DIG LDBL_DIG | Number of digits, q, such that a floating-point number with q decimal digits can be rounded into a floating-point representation and back without loss of precision. | 6 15 15 |
FLT_EPSILON DBL_EPSILON LDBL_EPSILON | Smallest positive number x, such that x + 1.0 is not equal to 1.0. | 1.192092896e–07F 2.2204460492503131e–016 2.2204460492503131e–016 |
FLT_GUARD |
| 0 |
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG | Number of digits in the radix specified by FLT_RADIX in the floating-point significant. The radix is 2; hence these values specify bits. | 24 53 53 |
FLT_MAX DBL_MAX LDBL_MAX | Maximum representable floating-point number. | 3.402823466e+38F 1.7976931348623158e+308 1.7976931348623158e+308 |
FLT_MAX_10_EXP DBL_MAX_10_EXP LDBL_MAX_10_EXP | Maximum integer such that 10 raised to that number is a representable floating-point number. | 38 308 308 |
FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP | Maximum integer such that FLT_RADIX raised to that number is a representable floating- point number. | 128 1024 1024 |
FLT_MIN DBL_MIN LDBL_MIN | Minimum positive value. | 1.175494351e–38F 2.2250738585072014e–308 2.2250738585072014e–308 |
FLT_MIN_10_EXP DBL_MIN_10_EXP LDBL_MIN_10_EXP | Minimum negative integer such that 10 raised to that number is a representable floating- point number. | –37 –307 –307 |
FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP | Minimum negative integer such that FLT_RADIX raised to that number is a representable floating-point number. | –125 –1021 –1021 |
FLT_NORMALIZE |
| 0 |
FLT_RADIX _DBL_RADIX _LDBL_RADIX | Radix of exponent representation. | 2 2 2 |
FLT_ROUNDS _DBL_ROUNDS _LDBL_ROUNDS | Rounding mode for floating-point addition. | 1 (near) 1 (near) 1 (near) |
Table 8 |
Take note that the information in the table may differ or updated in future or new versions of the product.