473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Questions

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
6 1610

"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

"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
> 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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.