"C + +" nested class, Friend

Source: Internet
Author: User

Huang Bongyong The introduction of nested classes I have doubts. Inside 11.9 says that when you create an object of a perimeter class, you execute the constructor of the nested class first, and then the constructor for the perimeter class, and the destructor executes in the opposite way.

But I programmed the experiment, and not the nested class's constructors are executed when the perimeter class object is created

#include <iostream>using namespacestd;classa{ Public:    intA, B, C; A () {a= b = c =0; cout<<"A construct"<<Endl; }classB { Public:        intA, B, C; B () {a= b = c =0; cout<<"B construct"<<Endl; }    };};intMain () {A CA; return 0;}

Friend Yuan

The purpose of declaring a friend is to allow a class's non-member functions or classes to access the private and protected members of the class object

1. A friend function cannot directly access a private member of a class only through the object of the class to access the private member.

Class A{int A; Pulbic:friend void G ();}

Then void g () {a=2;} Is wrong.

void G () {A m; m.a=2;} That's right.

2. Where friend appears is irrelevant to friends. That is, the friend declaration is the same in both the public private and protected locations.

3. To define a friend function or a friend class in a class then the class or function must be declared outside the perimeter class, notice that the declaration is not a definition, the location of the declaration does not matter, as long as it is declared before using the line.

(This is the case of the definition within the class, if it is a class outside the definition of only used in the class to declare that it is a friend can be)

class a{    int  A;     void f () {cout<<"F"<<// define friend function in class  int  main () {    void// accesses an example of a friend defined in a class, used before declaring, If the friend F () is declared in the global scope class    ;      // function f () the function f () can be used in global scope classes. If it is declared locally, it can only be used locally. }

     

"C + +" nested class, Friend

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.