指向抽象类的指针的数组:指向nullptr或不指向nullptr(C ++) [英] Array of Pointers to an Abstract Class: to nullptr or not to nullptr (C++)

查看:275
本文介绍了指向抽象类的指针的数组:指向nullptr或不指向nullptr(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个指向抽象类的指针数组,以找到一个空插槽,即检查一个元素是否指向派生类的对象。我的方法是创建数组并将每个元素设置为nullptr。然后,我可以检查该元素是否为nullptr。

I want to loop through an array of pointers to an abstract class to find an "empty" slot, that is to check whether an element points to an object of a derived class or not. My approach is to create the array and set each element to nullptr. Then, I can check if the element is nullptr.

这可行,但是有更好的方法吗?
编辑:我可以检查指向抽象类的指针数组中的第一个空元素(在该类中,派生类将定期由数组构造和指向,使该元素变为非空),没有在设置数组时将每个元素分配给nullptr,然后检查nullptr作为检查元素是否为空的方法?换句话说,我可以直接检查元素是否指向构造的基类吗?

This works, but is there a better way? Can I check for the first "empty" element in the array of pointers to an abstract class (in which derived classes will periodically be constructed and pointed to by the array, rendering that element not "empty"), without assigning each element to nullptr upon setting up the array and then checking for nullptr as a way to check if the element is "empty"? In other words, can I directly check whether the element points to a constructed base class or not?

Cat** catArray = new Cat*[200];
for(int i = 0; i < 200; i++){
   catArray[i] = nullptr;
}

for(int i = 0; i < 200; i++){
   if(catArray[i] == nullptr){ //edited, was typo as "!="
      AddRealCat(...);
      break;
   }
}      

我想知道是否有更简单的方法,在不将元素设置为nullptr的情况下,检查指向抽象类的指针数组中的元素是否指向派生类的对象还是仅仅是抽象指针。例如,在标准库中是否有 bool IsObject(ObjectType * ptr)或其他内容?

I wonder if there's an easier way to do this, to check whether an element in an array of pointers to an abstract class points to an object of a derived class or is just an abstract pointer, without setting the element to nullptr. Like, is there a bool IsObject(ObjectType* ptr) or something in the standard library?

而且,我想知道是否将每个元素设置为除了遍历数组并将元素设置为nullptr的计算成本之外,nullptr还会带来任何潜在的问题。

And, I wonder if setting each element to nullptr poses any potential problems, other than the computing cost of looping through the array and setting the elements to nullptr.

推荐答案

我猜使用 dynamic_cast 进行其他操作的真正简便方法是使用 std :: vector 而不是原始指针。

I guess the real easier way to do that other using dynamic_cast is using std::vector instead of a raw pointer.

示例代码

#include <string>
#include <iostream>
#include <vector>

struct Cat{
    virtual ~Cat() = default;
    virtual void meow() const = 0;
};

struct Meow : Cat{
    void meow() const override{ std::cout << "meow" << std::endl; }
};

int main()
{
    std::vector<Cat*> vec{100};
    vec[1] = new Meow();

    for(auto c : vec){
        if(auto m = dynamic_cast<Meow*>(c)){
            m->meow();
        }
    }

    // don't forget to release memory
    for(auto c : vec){
        delete c;
    }
}

实时示例

Live Example

现代版本,使用智能

#include <string>
#include <iostream>
#include <vector>
#include <memory>

struct Cat{
    virtual ~Cat() = default;
    virtual void meow() const = 0;
};
struct Meow : Cat{
    void meow() const override{ std::cout << "meow" << std::endl; }
};

int main()
{
    std::vector<std::unique_ptr<Cat>> vec{100};
    vec[1] = std::make_unique<Meow>();

    for(auto&& c : vec){
        if(auto m = dynamic_cast<Meow*>(c.get())){
            m->meow();
        }
    }

    // you don't need to manually clear the memory.
}

在线示例

Live Example

这篇关于指向抽象类的指针的数组:指向nullptr或不指向nullptr(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆