函数返回值的重要性

这是实际开发中遇到的问题,项目中函数不可能这么简单,很难定位Crash的原因,因此,函数尽量简洁、规范。
mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>

class MyDialog : public QDialog
{
    Q_OBJECT

public:
    MyDialog(QWidget *parent = nullptr);
    ~MyDialog();
    QString func(const QString &s);
};

#endif // MYDIALOG_H
 

mydialog.cpp
#include "mydialog.h"

MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
{
    QString s = "samp000";

    // 这个函数有时候没有返回值,如果没有返回值,程序就会崩溃
    QString getString = func(s);
}

MyDialog::~MyDialog()
{

}

QString MyDialog::func(const QString &s)
{
    if(s.isEmpty())
    {
        return s;
    }

}
 

main.cpp
#include "mydialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyDialog w;
    w.show();

    return a.exec();
}
 
 
效果展示


猜你喜欢

转载自www.cnblogs.com/samp000/p/12375017.html