C + + Learning path: the definition of member functions outside class and discussion of friends

Source: Internet
Author: User

Introduction: The difference between a member function definition within a class and outside a class is whether the expansion is inline.

Definitions are generally inline-expanded within a class, saving the overhead of calling functions. If the function body is too large, the compiler ignores the inline recommendations

If the definition is outside the class and needs to be declared within the class, the program runtime accesses the function in the way it is called, and is non-inline.

1#include <iostream>2#include <string>3#include <vector>4 using namespacestd;5 6 classStudent7 {8      Public:9 Student ();TenStudent (intIdConst stringNameintAge ); One         voidPrint ()Const; A     Private: -         intid_; -         stringname_; the         intAge_; - }; -  - student::student () +: Id_ (0), -Name_ (""), +Age_ (0) A { at } -  -Student::student (intID, -                  Const string&name, -                  intAge ) - : id_ (ID), in name_ (name), - Age_ (age) to { +      - } the  * voidStudent::p rint ()Const $ {Panax Notoginsengcout <<"Hello"<<Endl; -}

Let's discuss the relationship between friend and class object.

1#include <iostream>2#include <string>3#include <vector>4 using namespacestd;5 6 classTest7 {8      Public:9FriendclassOther;//declare that other is a friend of testTenFriendvoidBarConstTest &t); One     Private: A         intx_; -         intY_; - }; the  - class Other - { -      Public: +         voidFoo (Test &t) -         { +T.x_ =Ten; AT.y_ = -; at         } - }; -  - voidBarConstTest &t) - { -cout << t.x_ <<Endl; in } -  to intMainintargcConst Char*argv[]) + { - Test t; the     return 0; *}

Through the above code we can see that friends are directly accessible to the object's private data, you only need to declare in class XX class is my friend can, the keyword is friend.

Function can also be declared with the Friend keyword, the function can directly access the private data within the class.

C + + Learning path: the definition of member functions outside class and discussion of friends

Related Article

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.