Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Friend
- Friend and friend functions including friend class
- If the function A (non-member function) declared as class C of friend function, then the function A can directly access all the members of the class C object
- If the class is declared as a friend class A class C, so all members of the class A functions have direct access to all members of the class C object
- Friends violate encapsulation of object-oriented, but in some places frequently visited member variables can improve performance
#include <iostream>
using namespace std;
class Point {
// friend Point add(const Point &, const Point &);
friend class Math;
private:
int m_x;
int m_y;
public:
int getX() const { return this->m_x; };
int getY() const { return this->m_y; };
Point(int x, int y) :m_x(x), m_y(y) { }
};
class Math {
public:
Point add(const Point &point1, const Point &point2) {
return Point(point1.m_x + point2.m_x, point1.m_y + point2.m_y);
}
};
//Point add(const Point &point1, const Point &point2) {
// return Point(point1.m_x + point2.m_x, point1.m_y + point2.m_y);
//}
int main() {
Point point1(10, 20);
Point point2(20, 30);
Point point = add(point1, point2);
cout << endl;
getchar();
return 0;
}
Inner classes
- If the class A is defined within the class C, then A class is a class internal (nested classes)
- Features inner class
- Support public, protected, private rights
- All members of its functions can be accessed directly outside the class object (vice-versa)
- Member function directly without the class name, the object name to access static members of its outer class
- It will not affect the memory layout of the outer class
- May be defined outside of the outer class from outside the class declaration inside
#include <iostream>
using namespace std;
// Person
class Person {
private:
static int ms_legs;
static void other() {
}
int m_age;
void walk() {
}
// Car
class Car {
int m_price;
public:
Car() {
cout << "Car()" << endl;
}
void run() {
Person person;
person.m_age = 10;
person.walk();
ms_legs = 10;
other();
}
};
public:
Person() {
cout << "Person()" << endl;
Car car;
}
};
int Person::ms_legs = 2;
class Point {
class Math {
void test();
};
};
void Point::Math::test() {
}
int main() {
cout << sizeof(Person) << endl;
Person person;
Person::Car car;
getchar();
return 0;
}
Local class
- In a class of functions defined inside, called local class
- Features local class
- Scope is limited to the internal function resides
- All its members must be defined within the class, does not allow the definition of static member variables
- Member functions of local variables can not directly access functions (except static variable)
#include <iostream>
using namespace std;
int g_age = 20;
void test() {
int age = 10;
static int s_age = 30;
// 局部类
class Person {
public:
static void run() {
g_age = 30;
s_age = 40;
cout << "run()" << endl;
}
};
Person person;
Person::run();
}
int main() {
test();
getchar();
return 0;
}
Type Conversion
- C ++ is proposed using the C ++ style casting substituted C style cast
- C ++, there are four types of conversion specifier: static_cast, dynamic_cast, reinterpret_cast, const_cast