随笔分类 - C++
摘要:#include <iostream> class Person { public: Person(int age): m_age(age) { } ~Person() { } void func() { std::cout << "Person func" << std::endl; } void
阅读全文
摘要:#include <iostream> class Person { public: Person() { std::cout << "Person" << std::endl; } ~Person() { std::cout << "~Person" << std::endl; } private
阅读全文
摘要:class Person { private: int age; char sex; }; class Student: public Person { private: int cid; }; > cl /d1 reportSingleClassLayoutStudent D:\ConsoleAp
阅读全文
摘要:#include <iostream> class Add { public: int operator()(int a, int b) { std::cout << "operator()" << std::endl; return a + b; } }; int main() { using n
阅读全文
摘要:#include <iostream> class Person { public: Person(std::string name, int age): m_age(age), m_name(name) { } bool operator==(Person &person) { std::cout
阅读全文
摘要:#include <iostream> class Person { public: Person() { m_age = NULL; } Person(int age) { std::cout << "Person " << this << std::endl; m_age = new(int);
阅读全文
摘要:#include <iostream> class Person { public: void age_set(int age) { m_age = age; } int age_get() { return m_age; } /* 前置++ */ Person &operator++() { ++
阅读全文
摘要:class Person { friend std::ostream &operator<<(std::ostream &out, Person &person); public: void name_set(const std::string &name) { m_name = name; } v
阅读全文
摘要:#include <iostream> class Person { public: int age_get() { return m_age; } void age_set(int age) { m_age = age; } Person operator+(Person &person) { P
阅读全文
摘要:#include <iostream> class Person { //print是Person朋友 可以访问类中私有内容 friend void print(Person &person); public: Person(int age): age(age), sex(0) { } public
阅读全文
摘要:#include <iostream> class Person { public: //解决名称冲突 void age_set(int age) { //this指针指向 被调用的成员函数 所属的对象 this->age = age; } Person &age_add(int age) { th
阅读全文
摘要:#include <iostream> //每个空对象也分配1个字节空间,区分空对象内存位置。每个空对象也有一个独一无二的内存地址 class Person { }; class Student { //静态成员不属于类对象 static int voice; static void func();
阅读全文
摘要:class Person { public: Person() { std::cout << "Person()构造" << std::endl; } ~Person() { std::cout << "~Person()析构" << std::endl; } int age_get() { ret
阅读全文
摘要:#include <iostream> using namespace std; int main() { char buf[] = {1, 3, 4, 2, 6, 5}; int start = 0; int end = sizeof(buf) - 1; while(start < end) {
阅读全文
摘要:调用时机 使用一个已经创建完的对象来初始化一个新对象值传递的方式给函数传参以值方式返回局部对象 使用一个已经创建完的对象来初始化一个新对象 值传递的方式给函数传参 以值方式返回局部对象
阅读全文
摘要:默认的访问权限不同 struct 默认公有public class 默认私有private
阅读全文
摘要:int func(int a, int) { return a + 3; } int main() { int c = func(2, 100); cout << "c: " << c << endl; return 0; }c: 5
阅读全文
摘要:int func(int a, int b = 3) { return a + b; } int main() { int c = func(2); cout << "c: " << c << endl; return 0; }c: 5
阅读全文
摘要:int& func() { static int a = 10; return a; } int main() { int &a = func(); cout << "a: " << a << endl; func() = 20; /* 如果函数返回值是引用,这个函数调用可以作为左值 */ cout
阅读全文
摘要:int main() { int a = 10; //int &b; /* 引用必须初始化 */ int &b = a; /* 一旦初始化,不可更改 */ int c = 12; b = 12; /* 赋值 */ cout << "a: " << a << endl; cout << "b: " <
阅读全文