How to create array of pointers to object

Hi all,

I want to create an array of pointer objects to my class. Here it comes

myclass.hpp:

class tempclass
{
public:
int a;
map<int,string>m;
};

class myclass
{
public:
tempclass *t;
myclass()

};

myclass.cpp:

myclass::myclass()
{
int b;
cin>>b;
t=new tempclass[b]; // if b =5, i want to create 5 objects of tempclass
// at runtime
//After that i want to access t like t[0]->a=5;

t[0]->a=10; //giving error
t[1]->a=20; //giving error

}

Can any one help me in this how to overcome this problem. I want to access using the index like t[0],t[1] etc. Since the no of objects are passed at run time i dont know how to handle this.

Please help me to fix this.

Thanks in Advance
the statement:

t[0]->a=10;

is an error because t[0] is an object not a pointer to an object so the statement should rather be:

t[0].a = 10;


You're creating an array of objects, not an array of pointers to objects.
For that, you'd need
1
2
3
t=new tempclass*[b];
for (int a=0;a<b;a++)
    t[a]=new tempclass;
It can get messy quickly if you manage arrays directly. Why not use an STL container?
Topic archived. No new replies allowed.