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

观察者模式:为何Subject必须为抽象类?C++相关疑问

Answers to Your Observer Pattern & C++ Abstract Class Questions

Great questions—these touch on some critical C++ OOP details that are easy to overlook, especially when working with design patterns like Observer. Let's unpack each one:

1. Why do we need to implement the base class destructor when it's pure virtual? What happens if we don't?

First, let's clarify a key C++ rule: even a pure virtual destructor requires an implementation. Here's why:

When you destroy a derived class object via a pointer to its base class, C++ calls destructors in reverse order—first the derived class destructor, then the base class destructor. This holds true regardless of whether the base destructor is virtual, pure virtual, or non-virtual.

If you declare a pure virtual destructor without providing an implementation, the linker will throw an undefined reference error when it tries to resolve the call to the base destructor during object cleanup.

Take this example:

class Subject {
public:
    virtual ~Subject() = 0; // Pure virtual destructor
};

// Without this implementation, the linker will fail!
// Subject::~Subject() {}

class ConcreteSubject : public Subject {
private:
    std::vector<Observer*> observers;
};

int main() {
    Subject* sub = new ConcreteSubject();
    delete sub; // Triggers ConcreteSubject::~ConcreteSubject(), then needs Subject::~Subject()
    return 0;
}

Without the base destructor implementation, the linker can't find the code for Subject::~Subject(), which is required to complete the destruction chain.

Another way to think about it: pure virtual functions usually signal "this class can't be instantiated, and derived classes must override this". But destructors are special—they're always called when an object is destroyed, even for abstract base classes. So you can't skip the implementation here.

2. Why must the Subject base class be abstract?

The core idea here is about design intent and preventing misuse:

  • Subject is an abstract concept, not a concrete thing: In the Observer pattern, Subject represents a "thing that can be observed". You never want to create a raw Subject object—you always create specific implementations like TemperatureSensor or NewsFeed that have actual state to monitor and logic to notify observers. Making Subject abstract enforces this rule, since you can't instantiate an abstract class.

  • Enforces a consistent interface: A proper Subject abstract class should declare pure virtual methods like attach(Observer*), detach(Observer*), and notify(). This guarantees that all concrete Subjects follow the same interface, making your code predictable and easy to extend. Even if there are no other pure virtual methods (a rare edge case), making the destructor pure virtual still ensures the class can't be instantiated.

  • Avoids incomplete or useless objects: If Subject weren't abstract, someone might accidentally create a Subject instance. But this object would have no observers list, no state to track, and no way to notify anyone—so it's completely useless. Making it abstract eliminates this possibility.

Compare this to the Observer base class: you need it to define the update() interface for all observers. Similarly, Subject needs to be abstract to define the interface for all observable objects, and prevent invalid instantiation.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:39:58