我正在尝试在c++中使用唯一的指针实现一个简单的抽象工厂设计模式。我跟随这个链接作为参考(但修改了一点)。该链接使用原始指针,我想使用唯一的ptrs代替。请找到下面的代码。问题:分割故障。我不能指出毛病出在哪里。
devices.hpp:
#ifndef PHONE_HPP
#define PHONE_HPP
#include <iostream>
#include <string>
class phone
{
public:
virtual std::string get_name() const = 0;
};
class iphone : public phone
{
public:
std::string get_name() const override;
};
class android : public phone
{
public:
std::string get_name() const override;
};
class laptop
{
public:
virtual std::string get_name() const = 0;
};
class mac : public laptop
{
public:
std::string get_name() const override;
};
class chromebook : public laptop
{
public:
std::string get_name() const override;
};
devices.cpp
#include <iostream>
#include <string>
#include "phone.hpp"
std::string iphone::get_name() const {
return "iPhone X";
}
std::string android::get_name() const {
return "Pixel 6";
}
std::string mac::get_name() const {
return "Macbook Air";
}
std::string chromebook::get_name() const {
return "Chrome Flex";
}
device_manufacturer.hpp file:
#ifndef DEVICE_MANUFACTURER_HPP
#define DEVICE_MANUFACTURER_HPP
#include <iostream>
#include <string>
#include <memory>
#include "phone.hpp"
class device_manufacturer
{
public:
enum Manufacturer{APPLE, GOOGLE};
virtual std::unique_ptr<phone> get_phone() const = 0;
virtual std::unique_ptr<laptop> get_laptop() const = 0;
static std::unique_ptr<device_manufacturer> create_device(const Manufacturer& manf_input);
};
class Apple : public device_manufacturer
{
public:
std::unique_ptr<phone> get_phone() const override;
std::unique_ptr<laptop> get_laptop() const override;
};
class Google : public device_manufacturer
{
public:
std::unique_ptr<phone> get_phone() const override;
std::unique_ptr<laptop> get_laptop() const override;
};
device_manufacturer.cpp:
#include <iostream>
#include <string>
#include <memory>
#include "device_manufacturer.hpp"
#include "phone.hpp"
std::unique_ptr<device_manufacturer> device_manufacturer::create_device(const Manufacturer& manf_input){
std::unique_ptr<device_manufacturer> manf_ptr;
if (manf_input == Manufacturer::APPLE)
{
manf_ptr = std::make_unique<Apple>();
}
if (manf_input == Manufacturer::GOOGLE)
{
manf_ptr = std::make_unique<Google>();
}
return manf_ptr;
}
std::unique_ptr<phone> Apple::get_phone() const {
return std::unique_ptr<iphone>();
}
std::unique_ptr<phone> Google::get_phone() const {
return std::unique_ptr<android>();
}
std::unique_ptr<laptop> Apple::get_laptop() const {
return std::unique_ptr<mac>();
}
std::unique_ptr<laptop> Google::get_laptop() const {
return std::unique_ptr<chromebook>();
}
最后是客户端代码:
main.cpp:
#include <iostream>
#include <string>
#include <memory>
#include "device_manufacturer.hpp"
#include "phone.hpp"
int main(){
std::unique_ptr<device_manufacturer> manf = device_manufacturer::create_device(device_manufacturer::Manufacturer::APPLE);
std::cout << manf->get_laptop()->get_name() << std::endl;
return 0;
}
return std::unique_ptr<mac>()
(和其他)不分配内存;它只是说你有一个unique_ptr
指向mac
;但它实际上是用nullptr
初始化的。
改为return std::make_unique<mac>();
(其他3个函数也是如此)