C++ (Qt)class NetworkManager : public QObject{ .... std::unique_ptr<QNetworkAccessManager> m_manager; std::unique_ptr<QNetworkReply> m_reply;};
C++ (Qt) #include <iostream> class A{public: A( ) { std::cout << "constructor of A" << std::endl; } ~A( ) { std::cout << "destructor of A" << std::endl; }}; class B{public: B( ) { std::cout << "constructor of B" << std::endl; } ~B( ) { std::cout << "destructor of B" << std::endl; }}; void func( ){ A a; B b;} int main( ){ func( ); return 0;}
C++ (Qt)#ifndef NETWORKMANAGER_H#define NETWORKMANAGER_H #include <string>#include <QObject>#include <QNetworkAccessManager>#include <QNetworkReply>#include <QNetworkRequest>#include <QUrl>#include <QString>#include <QPointer>#include "EmptyArgument.h" class NetworkManager : public QObject{ Q_OBJECT public: NetworkManager( ) { m_manager = new QNetworkAccessManager( this ); } void fetch( const QString &url ) throw ( EmptyArgument ) { std::string functionName = "fetch()"; // Check the input argument if ( url.isEmpty( ) ) { throw EmptyArgument( functionName ); } m_reply = m_manager->get( QNetworkRequest( QUrl( url ) ) ); connect( m_reply, SIGNAL( finished( ) ), this, SLOT( replyFinished( ) ) ); } signals: void signalWithContent( QString content ); private slots: void replyFinished( ) { QByteArray data = m_reply->readAll( ); QString content( data ); emit signalWithContent( content ); } private: QPointer<QNetworkAccessManager> m_manager; QPointer<QNetworkReply> m_reply;}; #endif // NETWORKMANAGER_H