Explain the declaration of nested classes in C + + programming and the functions in them using the _c language

Source: Internet
Author: User

You can declare another class within the scope of one class. Such classes are referred to as "nested classes." Nested classes are considered to be within the bounds of the enclosing class and can be used within that scope. To refer to a range outside of the immediate enclosing scope of a nested class, you must use the fully qualified name.
The following example shows how to declare a nested class:

Nested_class_declarations.cpp
class Bufferedio
{public
:
  enum IOError {None, Access, general};

  Declare nested class Bufferedinput.
  Class Bufferedinput
  {public
  :
   int read ();
   int good ()
   {return
     _inputerror = None;
   }
  Private:
    ioerror _inputerror;

  Declare nested class Bufferedoutput.
  Class Bufferedoutput
  {
   //member list
  };

int main ()
{
}

The

declares Bufferedio::bufferedoutput and Bufferedio in Bufferedio::bufferedinput. These class names are not visible outside the scope of the class Bufferedio. However, an object of type Bufferedio does not contain any object of Bufferedinput or bufferedoutput type. A
nested class can only use the name, type name, static member name, and enumerator directly from the enclosing class. To use the names of other class members, you must use pointers, references, or object names.
in the previous Bufferedio example, the enumeration ioerror can be accessed directly by a member function, Bufferedio::bufferedinput, or bufferedio::bufferedoutput in a nested class, as shown in the function good.
Note that a
nested class declares a type only within the scope of a class. They do not cause the containing object of the nested class to be created. The preceding example declares two nested classes, but does not declare any objects of these class types.
When you declare a type name with a forward declaration, an exception that raises the scope visibility of the nested class declaration is raised. In this case, the class name declared by the forward declaration is visible outside the enclosing class, and its scope is defined as the smallest enclosing-class scope. For example,

Nested_class_declarations_2.cpp
class C
{public
:
  typedef class U u_t;//class u visible outside Clas s C Scope
  typedef class V {} v_t;//class V not visible outside class C
};

int main ()
{
  //Okay, forward declaration used above so file scope is used
  u* pu;

  Error, type name only exists in Class C scope
  u_t* pu2;//C2065

  //error, class defined above so Class C SCO PE
  v* PV;//C2065

  //Okay, fully qualified name
  c::v* Pv2;


Access rights in nested classes
embedding a class in another class does not provide special access to the member functions of the embedded class. Similarly, a member function of an enclosing class does not have special access to members of a nested class.
member functions in nested classes
member functions declared in a nested class can be defined in a file scope. The preceding example might have been written:

Member_functions_in_nested_classes.cpp
class Bufferedio
{public
:
  enum IOError {None, Access, General};
  Class Bufferedinput
  {public
  :
    int read ();//Declare but does not define member
    int good ();//Functions Rea D and good.
  Private:
    ioerror _inputerror;

  Class Bufferedoutput
  {
    //Member list.
}; Define member functions read and good in
//file scope.
int Bufferedio::bufferedinput::read ()
{return
  (1);
}

int Bufferedio::bufferedinput::good ()
{return
  _inputerror = = None;
}
int main ()
{
}

In the preceding example, the Qualified-type-name syntax is used to declare a function name. Statement:

Bufferedio::bufferedinput::read ()

Represents the Bufferedio function as a member of the Read class (in the scope of the Bufferedinput Class). "Because this declaration uses the Qualified-type-name syntax, the following form of construction is possible:

typedef bufferedio::bufferedinput BIO_INPUT;

int Bio_input::read ()

The above declaration is equivalent to the previous declaration, but it uses a typedef name instead of the class name.
friend functions in nested classes
a friend function declared in a nested class is considered to be within the scope of a nested class rather than an enclosing class. Therefore, the friend function does not gain specific access to members or member functions of the enclosing class. If you need to use a name declared in a nested class in a friend function, and the friend function is defined within the file scope, use the qualified type name, as follows:

Friend_functions_and_nested_classes.cpp

#include <string.h>

enum
{
  sizeofmessage = 255
};

Char *rgszmessage[sizeofmessage];

Class Bufferedio
{public
:
  class bufferedinput
  {public
  :
    friend int Getextendederrorstatus ();
    static char *message;
    static int messagesize;
    int imsgno;
  };

char *bufferedio::bufferedinput::message;
int bufferedio::bufferedinput::messagesize;

int Getextendederrorstatus ()
{
  int imsgno = 1;//Assign arbitrary value as message number

  strcpy_s (Buffer Edio::bufferedinput::message,
       bufferedio::bufferedinput::messagesize,
       Rgszmessage[imsgno]);

  return imsgno;
}

int main ()
{
}

The following code demonstrates a function getextendederrorstatus declared as a friend function. Copies a message from a static array to a class member in a function defined within a file scope. Note that the better implementation of Getextendederrorstatus is to declare it as:

int Getextendederrorstatus (char *message)

Using the preceding interfaces, many classes can use the service of this function by passing the memory location where the error message is to be replicated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.