跟强哥学C++ 源码 – 迷途通

跟强哥学C++ 源码

12月 4, 2022                      👁️ 56

B站视频地址:
https://www.bilibili.com/video/BV1h84y1k7v7

第3期 智能指针 实例入门

#include <iostream>
#include <memory>

using namespace std;

int foo(int x)
{
	int y = x * 2;
	int* p = new int[5];
	p[0] = y;
	//大量的逻辑
	delete[] p; // 手动释放内存

	return 0;
}

class Ball
{
public:
	Ball() {
		cout << "A ball appears." << endl;
	}

	~Ball()
	{
		cout << "A ball disappears." << endl;
	}
};

int main()
{
	shared_ptr<Ball> p1 = make_shared<Ball>();
	cout << p1.use_count() << endl;

	shared_ptr<Ball> p2 = p1;
	cout << p1.use_count() << "|" << p2.use_count() << endl;

	shared_ptr<Ball> p3 = p1;
	cout << p1.use_count() << "|" << p2.use_count() << "|" << p3.use_count() << endl;

	p1.reset();
	cout << p1.use_count() << "|" << p2.use_count() << "|" << p3.use_count() << endl;

	p2.reset();
	cout << p1.use_count() << "|" << p2.use_count() << "|" << p3.use_count() << endl;

	p3.reset();
	cout << p1.use_count() << "|" << p2.use_count() << "|" << p3.use_count() << endl;

	Ball* rp = p1.get();

	return 0;
}

第2期 C++ 基本数据类型及指针类型,函数指针

// C++ 基本数据类型及指针类型

#include <iostream>
#include <string>
using namespace std;

// 函数声明
int add(int a, int b);

void swap(int* p1, int* p2)
{
    int tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

void func(string str)
{
    cout << str << endl;
}

int main()
{
    int i = 0;
    double d = 123.456;
    char chr = 'a';
    char chrs[] = "C语言风格的字符串";
    string str = "C++风格的字符串";

    // 函数声明
    add(1, 2);

    // 指针,重要的知识点。指针就是地址。
    int* pInt = NULL; // 空指针
    pInt = &i;
    cout << *pInt << endl;
    cout << pInt << endl; // 内存地址,32位8,64位16

    const double* pD = NULL;//常量指针
    pD = &d;
    //*pD = 789.999;

    char* const pC = &chr; // 指针常量
    //pC = &chr;

    const string* const pStr = &str;

    int i1 = 6;
    int i2 = 8;
    swap(&i1, &i2);
    cout << i1 << endl;
    cout << i2 << endl;

    // 指向函数的指针
    void (*pFunc)(string);
    pFunc = func; // 函数名就是函数的地址
    pFunc("指向函数的指针");

    // 智能指针
    
    // sizeof
    cout << sizeof(int) << endl;
    cout << sizeof(double) << endl;
    cout << sizeof(char) << endl;
    cout << sizeof(string) << endl;
    system("pause");
}

int add(int a, int b)
{
    return a + b;
}

第1期 C++ Hello World Qt/QtCreator的安装

【腾讯文档】1001QtCreate安装
https://docs.qq.com/doc/DTnRsVHRjVkx5c0Rw

https://download.qt.io/archive/
长期支持版本
https://download.qt.io/archive/qt/5.9/5.9.9/
https://mirrors.tuna.tsinghua.edu.cn/qt/
https://download.qt.io/archive/qtcreator/4.14/4.14.2/

visual studio下载安装
https://visualstudio.microsoft.com/zh-hans/downloads/

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}
#include <QMessageBox>

void MainWindow::on_pushButton_clicked()
{
    QString dlgTitle = "消息框";
    QString strInfo = "Hello World";
    QMessageBox::information(this, dlgTitle, strInfo, QMessageBox::Ok, QMessageBox::NoButton);
}

发表回复

您的电子邮箱地址不会被公开。