Chapter 4 tools for large programs (11)

Source: Internet
Author: User

17.2.5 class, namespace, and scope

1. Search for real parameters and class-type parameters

Functions (including overload operators) in the namespace defined in the same namespace as the class parameter that accepts the class type parameter (or class type pointer to reference the parameter ), it is visible when a Class Object (or class type reference and pointer) is used as a real parameter.


If a function has a class-type parameter, the function is visible because non-member functions that are conceptually part of the class interface can be used without a separate using declaration. The ability to use non-member operations is particularly useful for operator functions.

// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H
 
Namespace andsoft
{
Class Class4 {};
Void Method4 (Class4 & c1 ){}
}
 
# Endif
// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H

Namespace andsoft
{
Class Class4 {};
Void Method4 (Class4 & c1 ){}
}

# Endifandresoft: Class4 c4 = randsoft: Class4 ();
Method4 (c4 );
Andresoft: Class4 c4 = andresoft: Class4 ();
Method4 (c4); 2. Implicit AIA and namespaces

When a class is declared as a friend function, the function declaration does not need to be visible. If no visible Declaration exists, you can put the declaration of this function or class into the peripheral scope. If the class is defined within the namespace, no other declared Enis are declared in the same namespace.

Because this element accepts the real parameters of the class type and is implicitly declared in the same namespace as the class, you do not need to use an explicit namespace qualifier when using it.

// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H
 
Namespace andsoft
{
Namespace Namespace6
{
Class Class5 {};
Class Class4 {
Friend class Class5;
Friend void Method4 (Class4 * c4 );
};
}
}
Void randsoft: Namespace6: Method4 (randsoft: Namespace6: Class4 * c4 ){}
 
# Endif
// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H

Namespace andsoft
{
Namespace Namespace6
{
Class Class5 {};
Class Class4 {
Friend class Class5;
Friend void Method4 (Class4 * c4 );
};
}
}
Void randsoft: Namespace6: Method4 (randsoft: Namespace6: Class4 * c4 ){}

# Endifandresoft: Namespace6: Class4 * c4 = new randsoft: Namespace6: Class4 ();
Randsoft: Namespace6: Method4 (c4 );
Method4 (c4 );
Randsoft: Namespace6: Class4 * c4 = new randsoft: Namespace6: Class4 ();
Randsoft: Namespace6: Method4 (c4 );
Method4 (c4 );

From xufei96's column
 

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.