By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Questions

P: n/a
ikl
Is it right to define an array of pointers pointing to a list of objects
like this?

class A
{
};

A* ptr[15];

int i = 0;
for(i=0; i<15; ++i){
ptr[i] = new A();
}

Is it possible to define a list of references referring to a list of
objects?

Thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"ikl" <ik***@dsp.com> wrote in message
news:1v********************@bgtnsc05-news.ops.worldnet.att.net...
Is it right to define an array of pointers pointing to a list of objects
like this?

class A
{
};

A* ptr[15];

int i = 0;
for(i=0; i<15; ++i){
ptr[i] = new A();
}
Whether its right or not depends on your requirements. It's not something I
would recommend to a newbie. You should use a vector.

Is it possible to define a list of references referring to a list of
objects?


It's impossible to create an array of references, which is what I think you
are asking.

john
Jul 22 '05 #2

P: n/a

"ikl" <ik***@dsp.com> wrote in message
news:1v********************@bgtnsc05-news.ops.worldnet.att.net...
Is it right to define an array of pointers pointing to a list of objects
like this?

class A
{
};

A* ptr[15];

int i = 0;
for(i=0; i<15; ++i){
ptr[i] = new A();
}
Whether its right or not depends on your requirements. It's not something I
would recommend to a newbie. You should use a vector.

Is it possible to define a list of references referring to a list of
objects?


It's impossible to create an array of references, which is what I think you
are asking.

john
Jul 22 '05 #3

P: n/a
> Is it right to define an array of pointers pointing to a list of objects
like this?

class A
{
};

A* ptr[15];

int i = 0;
for(i=0; i<15; ++i){
ptr[i] = new A();
}
"Right" depends on what you want to do. This code compiles fine, and
probably does what you want, except that you forgot to delete the content
of the array :

for (int i=0; i<15; ++i)
delete ptr[i];

But I would recommend using std::vector :

# include <vector>

class A
{
public:
A(int a, int b);
};

int main()
{
std::vector<A> v;

v.push_back( A() );
v.push_back( A(1, 3) );
}

Look in your textbook for some more informations.
Is it possible to define a list of references referring to a list of
objects?


No.
Jonathan
Jul 22 '05 #4

P: n/a
> Is it right to define an array of pointers pointing to a list of objects
like this?

class A
{
};

A* ptr[15];

int i = 0;
for(i=0; i<15; ++i){
ptr[i] = new A();
}
"Right" depends on what you want to do. This code compiles fine, and
probably does what you want, except that you forgot to delete the content
of the array :

for (int i=0; i<15; ++i)
delete ptr[i];

But I would recommend using std::vector :

# include <vector>

class A
{
public:
A(int a, int b);
};

int main()
{
std::vector<A> v;

v.push_back( A() );
v.push_back( A(1, 3) );
}

Look in your textbook for some more informations.
Is it possible to define a list of references referring to a list of
objects?


No.
Jonathan
Jul 22 '05 #5

P: n/a
ikl wrote:
Is it right to define an array of pointers pointing to a list of objects
like this?

class A
{
};

A* ptr[15];

int i = 0;
for(i=0; i<15; ++i){
ptr[i] = new A();
}


If the items are all the same type, as is the case here, then there's no
need for pointers at all. You could simply create the array this way:

A some_array[15];

Or use a std::vector, as others suggested. This is preferable in most
cases, because of the increased flexibility and ease of use.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #6

P: n/a
ikl wrote:
Is it right to define an array of pointers pointing to a list of objects
like this?

class A
{
};

A* ptr[15];

int i = 0;
for(i=0; i<15; ++i){
ptr[i] = new A();
}


If the items are all the same type, as is the case here, then there's no
need for pointers at all. You could simply create the array this way:

A some_array[15];

Or use a std::vector, as others suggested. This is preferable in most
cases, because of the increased flexibility and ease of use.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.