9月6日(∠・ω<)⌒☆

1、手写unique_ptr指针指针

#include <iostream>
#include <stdexcept>
 
template <typename T>
class unique_ptr 
{
public:
    // 构造函数
    explicit unique_ptr(T* ptr = nullptr) : m_ptr(ptr) {}
 
    // 析构函数
    ~unique_ptr() 
    {
        delete m_ptr;
    }
 
    // 禁止复制构造函数
    unique_ptr(const unique_ptr&) = delete;
 
    // 禁止复制赋值运算符
    unique_ptr& operator=(const unique_ptr&) = delete;
 
    // 移动构造函数
    unique_ptr(unique_ptr&& other) noexcept : m_ptr(other.m_ptr) 
    {
        other.m_ptr = nullptr;
    }
 
    // 移动赋值运算符
    unique_ptr& operator=(unique_ptr&& other) noexcept 
    {
        if (this != &other) {
            delete m_ptr;
            m_ptr = other.m_ptr;
            other.m_ptr = nullptr;
        }
        return *this;
    }
 
    // 重载*和->运算符
    T& operator*() const 
    {
        return *m_ptr;
    }
 
    T* operator->() const 
    {
        return m_ptr;
    }
 
    // 获取原始指针
    T* get() const 
    {
        return m_ptr;
    }
 
    // 释放所有权
    T* release() 
    {
        T* temp = m_ptr;
        m_ptr = nullptr;
        return temp;
    }
 
    // 重置智能指针
    void reset(T* ptr = nullptr) 
    {
        T* old_ptr = m_ptr;
        m_ptr = ptr;
        delete old_ptr;
    }
 
private:
    T* m_ptr;
};
 
int main() 
{
    unique_ptr<int> ptr1(new int(10));
    std::cout << *ptr1 << std::endl;
 
    unique_ptr<int> ptr2 = std::move(ptr1);
    std::cout << *ptr2 << std::endl; // 输出 10
    std::cout << *ptr1 << std::endl; // 不会输出什么东西
 
    return 0;
}

2、手写登录界面

头文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QPushButton>
#include<QLabel>
#include<QLineEdit>
#include<QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();



public slots:
    void my_slot();


private:
    Ui::Widget *ui;

    QLineEdit *usernameEdit;       //账号
    QLineEdit *passwordEdit;       //密码

    QPushButton *loginButton;        //自定义登录按钮
    QPushButton *quitButton;          //自定义退出按钮

};
#endif // WIDGET_H

源文件

#include "widget.h"
#include "ui_widget.h"

#include<QLineEdit>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QLabel *lab1 = new QLabel("账号:",this);
    lab1->move(200,200);

    QLabel *lab2 = new QLabel("密码:",this);
    lab2->move(lab1->x(),lab1->y()+40);

    loginButton = new QPushButton("登录",this);
    loginButton->resize(80,40);
    loginButton->move(lab1->x()+120,lab1->y()+100);

    quitButton = new QPushButton("取消",this);
    quitButton->resize(80,40);
    quitButton->move(loginButton->x()+100,loginButton->y());

    usernameEdit = new QLineEdit;
    usernameEdit->setParent(this);
    usernameEdit->resize(300,30);
    usernameEdit->move(lab1->x()+lab1->width()+2,lab1->y()-10);


    passwordEdit = new QLineEdit("密码",this);
    passwordEdit->resize(300,30);
    passwordEdit->move(lab2->x()+lab1->width()+2,lab2->y()-10);
    passwordEdit->clear();
    passwordEdit->setPlaceholderText("请输入密码");
    passwordEdit->setEchoMode(QLineEdit::Password);

    connect(loginButton, &QPushButton::clicked, this, &Widget::my_slot);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::my_slot()
{
    // 获取行编辑器中的内容
    QString account = usernameEdit->text();
    QString password = passwordEdit->text();

    // 判断账号和密码是否正确
    if (account == "123456" && password == "123456")
    {
        // 登录成功
        qDebug() << "登录成功";
        this->close();
    }
    else
    {
        // 登录失败
        passwordEdit->setPlaceholderText("登录失败,请重新输入");
        passwordEdit->clear();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值