简体   繁体   English

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

[英]Pointer to array of base class, populate with derived class

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 BaseClass s methods through the DerivedClass , but by pointer and the pointer is an array of DerivedClass s? ...这样我就可以通过DerivedClass访问BaseClass的方法,但是通过指针和指针是DerivedClass的数组?

Your array is of the wrong type: it stores BaseClass object instances instead of pointers to them.您的数组类型错误:它存储BaseClass对象实例而不是指向它们的指针 Since BaseClass seems to be abstract, the compiler complains that it cannot default-construct instances to fill your array.由于BaseClass似乎是抽象的,编译器抱怨它不能默认构造实例来填充数组。

Even if BaseClass were not abstract, using arrays polymorphically is a big no-no in C++ so you should do things differently in any case.即使BaseClass不是抽象的,多态地使用数组在 C++ 中也是一个很大的BaseClass ,所以在任何情况下你都应该做不同的事情。

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::vector instead of plain arrays and smart pointers (such as std::shared_ptr ) instead of dumb pointers.也就是说,大多数时候最好使用std::vector而不是普通数组和智能指针(例如std::shared_ptr )而不是哑指针。 Using these tools instead of manually writing code will take care of a host of issues transparently at an extremely small runtime cost.使用这些工具而不是手动编写代码将以极小的运行成本透明地处理大量问题。

It is C++ use std::vector instead of simple array: C++ 使用std::vector而不是简单数组:

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

As Kerrek SB noticed 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()) );

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.在 C++ 中,这是通过使用 std::vector 或 std::array 以及某种智能指针来完成的。 For example :例如 :

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

This was the answer (from Rubby)这就是答案(来自 Rubby)

BaseClass* Base[2];

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

Define a pointer array , the pointer type is BaseClass.定义一个指针数组,指针类型为BaseClass。 And assign the pointer to the derivedclass to the elements of the array.并将派生类的指针赋给数组的元素。 just like:就像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM