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

子类指针能否赋值给父类变量?父类数组存子类指针合法性及实现

Hey there! Let's walk through your questions step by step—they’re all centered around C++ polymorphism, which is a key concept for building flexible, reusable code.

1. 子类指针能否赋值给父类类型的变量?

Absolutely! This is called upcasting, and it’s not just allowed—it’s a fundamental part of how polymorphism works in C++. The conversion happens implicitly and safely.

For example, if you have a base class A and a derived class Aa:

Aa* aa_ptr = new Aa();
A* a_ptr = aa_ptr; // No explicit cast needed—this is fully legal

One important note: Once you assign a subclass pointer to a parent class pointer, you can only access members defined in the parent class (including virtual functions that the subclass has overridden). You can’t directly access subclass-specific members through this parent pointer unless you later perform a downcast (using dynamic_cast, which you should always check for validity to avoid undefined behavior).

2. 存储子类对象指针的数组合法性及对应函数返回类型

Let’s break this into clear parts:

声明A类类型的数组是否合法?

No, and you definitely don’t want to do this. If you declare an array like A arr[10];, trying to assign subclass objects to it will cause object slicing: the subclass-specific parts of the object get "cut off," leaving only the parent class portion. This completely destroys the polymorphism you’re aiming for.

如何正确实现存储Aa和Ab对象指针的需求?

Instead of storing objects directly, store pointers to the parent class. An array of A* is fully legal and exactly what you need:

A* arr[2];
arr[0] = new Aa(); // Points to an Aa object
arr[1] = new Ab(); // Points to an Ab object

For modern C++, I’d recommend using smart pointers (like std::unique_ptr<A> or std::shared_ptr<A>) instead of raw pointers to avoid manual memory management headaches:

#include <memory>

std::unique_ptr<A> arr[2];
arr[0] = std::make_unique<Aa>();
arr[1] = std::make_unique<Ab>();

对应的查找函数应该返回什么类型?

If your array uses raw pointers (A*), the function should return A*. If you’re using smart pointers, return the corresponding smart pointer type (e.g., std::unique_ptr<A>).

Here’s an example with raw pointers (don’t forget to add bounds checking in real code!):

A* get_object(A* arr[], int index) {
    // Add check: if index is out of bounds, handle error (return nullptr, throw, etc.)
    return arr[index];
}

When you get this A* pointer back, you can call virtual functions defined in A and the correct subclass implementation will run. If you need to access subclass-specific members, use dynamic_cast to safely convert it to the subclass pointer (always check if the result is nullptr to avoid crashes):

Aa* aa_subclass_ptr = dynamic_cast<Aa*>(get_object(arr, 0));
if (aa_subclass_ptr != nullptr) {
    // Access Aa-specific members here
}

内容的提问来源于stack exchange,提问作者J. Cal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:35:13