就像在标题中一样。
我的目标是在不重新创建对象的情况下获取对象,显然我可以使用指针但是
1) 我喜欢为工厂对象、数据库处理程序等保留指针
2)我想学点东西,而不是走简单的路
所以代码和错误在底部,感谢您的时间和建议:)
代码中的某个地方
QTcpSocket* socket = (QTcpSocket*)this->sender();
Request request(socket);
Response response(request, this->managerFactory);
响应.h
#ifndef RESPONSE_H
#define RESPONSE_H
#include <QString>
#include "Request.h"
#include "../manager/ManagerFactory.h"
class Response {
public:
Response(const Request &request, ManagerFactory *managerFactory);
QString getValue();
private:
QString generateJsonHeader();
Request &request;
ManagerFactory *managerFactory;
};
#endif /* RESPONSE_H */
共振.cpp
#include "Response.h"
Response::Response(const Request &request, ManagerFactory *managerFactory)
{
this->request = request;
this->managerFactory = managerFactory;
}
请求.h
#ifndef REQUEST_H
#define REQUEST_H
#include <QTcpSocket>
#include <QStringList>
#include <QString>
#include <QMap>
class Request {
public:
Request(QTcpSocket* socket);
bool hasParameter(QString paramName);
QString getParameter(QString paramName);
private:
void loadParameters(QTcpSocket* socket);
QMap<QString, QString> parameters;
};
#endif /* REQUEST_H */
请求.cpp
#include "Request.h"
Request::Request(QTcpSocket* socket)
{
this->loadParameters(socket);
}
这是我面临的错误
source/server/Response.cpp: In constructor ‘Response::Response(const Request&, ManagerFactory*)’:
source/server/Response.cpp:3:1: error: uninitialized reference member ‘Response::request’ [-fpermissive]
make[2]: *** [build/Release/GNU-Linux-x86/Response.o] Error 1