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