我不知道为什么它无法正常工作,在删除时,将新策略分配给基指针都会导致崩溃。
SendStrategy.h
#ifndef SENDSTRATEGY_H
#define SENDSTRATEGY_H
#include <QByteArray>
#include "QDebug"
class SendStrategy
{
public:
virtual ~SendStrategy();
virtual void sendDatagramData(QByteArray array) = 0;
};
#endif // SENDSTRATEGY_H
SendStrategy.cpp
#include "SendStrategy.h"
SendStrategy::~SendStrategy()
{
}
ConcreteStrategy.h
#ifndef SOCKETUDPSENDSTRATEGY_H
#define SOCKETUDPSENDSTRATEGY_H
#include <QObject>
#include <QUdpSocket>
#include <Utilities/SendStrategy.h>
class SocketUdpSendStrategy: public SendStrategy
{
private:
QUdpSocket* socket;
QHostAddress address;
public:
SocketUdpSendStrategy();
~SocketUdpSendStrategy();
void sendDatagramData(QByteArray array);
};
#endif // SOCKETUDPSENDSTRATEGY_H
SetStrategyMethod实现
void SendModel::setSendStrategy(SendStrategy* newStrategy)
{
delete sendStrategy;
sendStrategy = newStrategy;
}
那我怎么称呼那个函数
sendModel->setSendStrategy(new SocketUdpSendStrategy());
默认为SendStrategy* sendStrategy = nullptr;