You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用unique_ptr实现抽象工厂模式时触发Segmentation fault问题排查

使用unique_ptr实现抽象工厂模式时出现Segmentation Fault的排查

我尝试在C++中用unique_ptr实现简单的抽象工厂设计模式,参考相关文章并将原示例的裸指针改为unique_ptr。目前代码运行出现Segmentation fault,无法定位故障原因,请求协助排查。


代码文件

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;
};

#endif

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

#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;
};

#endif

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;
}

故障原因及修复方案

1. 核心故障:空unique_ptr导致的空指针访问

在device_manufacturer.cpp的产品创建函数中,比如Apple::get_laptop(),你使用return std::unique_ptr<mac>();的写法,仅创建了一个空的unique_ptr(未指向任何实际的mac对象)。当在main中调用manf->get_laptop()->get_name()时,本质是通过空指针调用虚函数,直接触发Segmentation Fault。

修复方式:使用std::make_unique创建实际的对象实例,替换原有的空unique_ptr构造写法:

std::unique_ptr<phone> Apple::get_phone() const {
    return std::make_unique<iphone>();
}

std::unique_ptr<phone> Google::get_phone() const {
    return std::make_unique<android>();
}

std::unique_ptr<laptop> Apple::get_laptop() const {
    return std::make_unique<mac>();
}

std::unique_ptr<laptop> Google::get_laptop() const {
    return std::make_unique<chromebook>();
}

2. 潜在问题:头文件缺失闭合宏

原devices.hpp末尾缺少#endif,会导致头文件重复包含时出现编译错误,需在文件末尾添加#endif(已在上方代码示例中补全)。

内容的提问来源于stack exchange,提问作者BoringSession

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 14:15:54