Hi, do not know how possible is this but here it goes, i'm trying to declare an array once a program is running. This is how i'm thinking:

[LIST=1]
[*]int size;
[*]cout << "Enter size of array: ";
[*]cin >> size;
[*]Holding *holdLib[size];   //this is a base class pointer array
[/LIST]

i've tried declaring a const int with the value of size to the pass the const int into the array....any thoughts???.....

Recommended Answers

All 6 Replies

use the new operator to allocate dynamic memory Holding *holdLib = new Holding[size];

i forgot to mention that Holding is an abstract class so it will not work, any other suggestions???

Abstract classes (I presume you mean it contains one or more pure virtual functions) can not be instantiated by themselves. So there is no way to make an array of them. You could, however make a vector of them vector<Holding*> array;

#include <vector>
using namespace std;

class A
{
public:
    A() {x = 0;}
    virtual void foo() = 0;
private:
    int x;
};

int main()
{
    vector<A*> array;
}

Thanks for the suggestion, but we are not allow to use vectors for the homework, i guess that for the tools that we are allow to use, i cannot do as i like :(

You could create an array of pointers

class Holding
{
   // blabla
};
class Derived :public Holding
{
  // blabla
};

Holding **array = new Holding*[size];
// add a class of type derived
Derived* dv = new Derived;
array[0] = dv;
commented: patient, multiple solutions... +5

P-to-P why !!! thanks, i forgot about that option

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.