简体   繁体   中英

Parent class turned into subclass Pointers C++

I have a pointer that is set to a new array of objects:

Animal* pAnimals = new Animal[10];

The Animal class has a subclass called Dog , with some more specific functionality.

I need to have some of the objects in the pAnimals pointer array to be dogs, but not all of them. Here is what I have tried so far, but with memory leaks:

Animal* pAnimals = new Animal[10];

Animal ** pCurrent = &pAnimals;

for (size_t i = 0; i < 8; i++)
{
    *(pCurrent + i) = new Dog();
} 

I know that Animal* pAnimals = new Animal[10]; is creating 10 Animal objects on the heap, so I will need to delete them later.

You have to create an array of Animal - pointers first.

Animal **pAnimaly = new Animal*[10];

Then you can create instances of subclasses of Animal like Dog . As others suggested, you should consider using std::vector or std:array instead of ordinary arrays but I assume it's just a lecture so doing some low level stuff for learning is probably fine.

#include <iostream>

using namespace std;

class Animal {};
class Dog : public Animal {};

int main()
{
    Animal **pAnimaly = new Animal*[10];

    for (size_t i = 0; i < 10; i++)
    {
       cout << pAnimaly[i] << endl;
    } 

    cout << "---" << endl;

    for (size_t i = 0; i < 8; i++)
    {
       pAnimaly[i] = new Dog();
    } 

    for (size_t i = 0; i < 10; i++)
    {
       cout << pAnimaly[i] << endl;

       if (pAnimaly[i] != nullptr) {
           cout << "Freeing memory.." << endl;
           delete pAnimaly[i];       
       }
    } 

    return 0;
}

Take a look at this. Maybe it'll help you.
There is an Animal class (quite trvial one but that's just an example) and there are two derived classes. Then in main function there is a vector of shared_ptr s to Animal s. Some of those pointers point to Animal objects and other to Dog s or Cat s.

#include <random>
#include <vector>
#include <memory>

class Animal {
public:
    virtual ~Animal() = default;
};

class Dog : public Animal {
};

class Cat : public Animal {
};

int main() {
    std::random_device gen;
    std::uniform_int_distribution<unsigned int> dist(0, 2);
    std::vector<std::shared_ptr<Animal>> animals;
    for(unsigned int i = 0; i < 10; ++i) {
        animals.push_back([&] () -> std::shared_ptr<Animal> {
            switch(dist(gen)) {
                case 0:
                    return std::make_shared<Animal>();

                case 1:
                    return std::make_shared<Dog>();

                case 2:
                    return std::make_shared<Cat>();
            }
        }());
    }
}
Animal* animals[10];
for(int i = 0; i < 8; i++)
{
    animals[i] = new Dog();
}
animals[8] = new Animal();
animals[9] = new Animal();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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