C++ 指向基类数组的指针,用派生类填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13048301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:54:30  来源:igfitidea点击:

Pointer to array of base class, populate with derived class

c++arraysinheritancepointersderived-class

提问by Deukalion

If I have a base class, with only virtual methods and 2 derived classes from the base class, with those virtual methods implemented.

如果我有一个基类,只有虚拟方法和 2 个来自基类的派生类,并实现了这些虚拟方法。

How do I:

我如何能:

 // causes C2259
 BaseClass* base = new BaseClass[2];

 BaseClass[0] = new FirstDerivedClass;
 BaseClass[1] = new SecondDerivedClass;

or:

或者:

// causes "base is being used without being initialized"
BaseClass* base;
// causes CC59 again
BaseClass* base = new BaseClass;

base[0] = FirstDerivedClass();
base[1] = SecondDerivedClass();

(or something similar)

(或类似的东西)

...so that I can access the BaseClasss methods through the DerivedClass, but by pointer and the pointer is an array of DerivedClasss?

...这样我就可以BaseClass通过DerivedClass, 但通过指针访问s 方法,而指针是一个DerivedClasss数组?

采纳答案by Jon

Your array is of the wrong type: it stores BaseClassobject instancesinstead of pointersto them. Since BaseClassseems to be abstract, the compiler complains that it cannot default-construct instances to fill your array.

您的数组类型错误:它存储BaseClass对象实例而不是指向它们的指针。由于BaseClass似乎是抽象的,编译器抱怨它不能默认构造实例来填充您的数组。

Even if BaseClasswere not abstract, using arrays polymorphically is a big no-noin C++ so you should do things differently in any case.

即使BaseClass不是抽象的,多态地使用数组在 C++ 中也是一个很大的禁忌,所以在任何情况下你都应该做不同的事情。

Fix this by changing the code to:

通过将代码更改为以下内容来解决此问题:

BaseClass** base = new BaseClass*[2];

base[0] = new FirstDerivedClass;
base[1] = new SecondDerivedClass;

That said, most of the time it is preferable to use std::vectorinstead of plain arrays and smart pointers (such as std::shared_ptr) instead of dumb pointers. Using these tools instead of manually writing code will take care of a host of issues transparently at an extremely small runtime cost.

也就是说,在大多数情况下,最好使用std::vector而不是普通数组和智能指针(例如std::shared_ptr)而不是哑指针。使用这些工具而不是手动编写代码将以极小的运行成本透明地处理大量问题。

回答by Denis Ermolin

It is C++ use std::vectorinstead of simple array:

它是 C++ 使用std::vector而不是简单的数组:

std::vector<BaseClass*> base;
base.push_back(new FirstDerivedClass());
base.push_back(new SecondDerivedClass());

As Kerrek SBnoticed safest method is to use std::unique_ptr:

由于Kerrek SB注意到最安全的方法是使用std::unique_ptr

std::vector<std::unique_ptr<BaseClass> > base;
base.push_back( std_unique_ptr<BaseClass>(new FirstDerivedClass()) );
base.push_back( std_unique_ptr<BaseClass>(new SecondDerivedClass()) );

回答by B?ови?

If your BaseClass contains pure virtual methods, this will fail to compile :

如果你的 BaseClass 包含纯虚方法,这将无法编译:

BaseClass* base = new BaseClass[2];

If it doesn't, you are going to get memory leak.

如果没有,您将获得内存泄漏。

In c++, this is done by using std::vector or std::array, with some kind of smart pointer. For example :

在 C++ 中,这是通过使用 std::vector 或 std::array 以及某种智能指针来完成的。例如 :

std::vector< std::shared_ptr< BaseClass > > arr( 2 );
arr[0].reset( new FirstDerivedClass() );
arr[1].reset( new SecondDerivedClass() );

回答by Deukalion

This was the answer (from Rubby)

这就是答案(来自 Rubby)

BaseClass* Base[2];

Base[0] = new FirstDerivedClass;
Base[1] = new SecondDerivedClass;

回答by user1203650

Define a pointer array , the pointer type is BaseClass. And assign the pointer to the derivedclass to the elements of the array. just like:

定义一个指针数组,指针类型为BaseClass。并将派生类的指针赋给数组的元素。就像:

BaseClass* base [2];
base[0] = new FirstDerivedClass;
base[1] = new SecondDerivedClass;