array of objects of parametrized constructor

A

asit

consider this class

class A
{
int a;
public:
A(int x):a(x)
{
cout<<"Parametrized constructor"<<endl;
}
};

How can I create array of objects ??

I can't add the default constructor and overload the parametrized constructor.
 
A

Alf P. Steinbach /Usenet

* asit, on 03.05.2011 20:32:
consider this class

class A
{
int a;
public:
A(int x):a(x)
{
cout<<"Parametrized constructor"<<endl;
}
};

How can I create array of objects ??

I can't add the default constructor and overload the parametrized
constructor.

The simplest solution is to use std::vector, from the <vector> header.

E.g.

std::vector<A> myArray;

myArray.push_back( A( 1 ) );
//...

And that's what you should be doing anyway, as a beginner. Don't mess around
with raw arrays. For that matter, don't add assembly language stubs either. ;-)


Cheers & hth.,

- Alf
 
N

Noah Roberts

consider this class

class A
{
int a;
public:
A(int x):a(x)
{
cout<<"Parametrized constructor"<<endl;
}
};

How can I create array of objects ??

I can't add the default constructor and overload the parametrized constructor.

You have to initialize all elements:

A as[] = { A(0), A(1) };

Alternatively you could just use a vector and initialize them all to
some value:

std::vector<A> as(10,A(0));
 
I

Ian Collins

* asit, on 03.05.2011 20:32:

The simplest solution is to use std::vector, from the<vector> header.

E.g.

std::vector<A> myArray;

myArray.push_back( A( 1 ) );
//...

And that's what you should be doing anyway, as a beginner. Don't mess around
with raw arrays. For that matter, don't add assembly language stubs either. ;-)

In this case, using a temporary array as a helper would be fine:

A tmp[] = { 1,2,3,4 };
std::vector<A> a(tmp, tmp+(sizeof(tmp)/sizeof(A)) );
 
A

Alf P. Steinbach /Usenet

* Ian Collins, on 04.05.2011 00:18:
* asit, on 03.05.2011 20:32:

The simplest solution is to use std::vector, from the<vector> header.

E.g.

std::vector<A> myArray;

myArray.push_back( A( 1 ) );
//...

And that's what you should be doing anyway, as a beginner. Don't mess around
with raw arrays. For that matter, don't add assembly language stubs either. ;-)

In this case, using a temporary array as a helper would be fine:

A tmp[] = { 1,2,3,4 };
std::vector<A> a(tmp, tmp+(sizeof(tmp)/sizeof(A)) );

I disagree.

Even an experienced programmer like yourself gets it, well not technically wrong
but pretty imperfect. :-(

If done, it would better look like this:

int const tmp[] = { 1,2,3,4 };
std::vector<A> a(tmp, tmp+(sizeof(tmp)/sizeof(*tmp)) );

But since it's so easy to get wrong, and since it creates an association where
use of raw arrays seems to be OK in general, I recommend not doing it.


Cheers,

- Alf
 
I

Ian Collins

* Ian Collins, on 04.05.2011 00:18:
* asit, on 03.05.2011 20:32:
consider this class

class A
{
int a;
public:
A(int x):a(x)
{
cout<<"Parametrized constructor"<<endl;
}
};

How can I create array of objects ??

I can't add the default constructor and overload the parametrized
constructor.

The simplest solution is to use std::vector, from the<vector> header.

E.g.

std::vector<A> myArray;

myArray.push_back( A( 1 ) );
//...

And that's what you should be doing anyway, as a beginner. Don't mess around
with raw arrays. For that matter, don't add assembly language stubs either. ;-)

In this case, using a temporary array as a helper would be fine:

A tmp[] = { 1,2,3,4 };
std::vector<A> a(tmp, tmp+(sizeof(tmp)/sizeof(A)) );

I disagree.

Even an experienced programmer like yourself gets it, well not technically wrong
but pretty imperfect. :-(

If done, it would better look like this:

int const tmp[] = { 1,2,3,4 };
std::vector<A> a(tmp, tmp+(sizeof(tmp)/sizeof(*tmp)) );

You've been secretly reading c.l.c, haven't you Alf??
 
A

Alf P. Steinbach /Usenet

* Ian Collins, on 04.05.2011 11:24:
* Ian Collins, on 04.05.2011 00:18:
On 05/ 4/11 07:29 AM, Alf P. Steinbach /Usenet wrote:
* asit, on 03.05.2011 20:32:
consider this class

class A
{
int a;
public:
A(int x):a(x)
{
cout<<"Parametrized constructor"<<endl;
}
};

How can I create array of objects ??

I can't add the default constructor and overload the parametrized
constructor.

The simplest solution is to use std::vector, from the<vector> header.

E.g.

std::vector<A> myArray;

myArray.push_back( A( 1 ) );
//...

And that's what you should be doing anyway, as a beginner. Don't mess around
with raw arrays. For that matter, don't add assembly language stubs either. ;-)

In this case, using a temporary array as a helper would be fine:

A tmp[] = { 1,2,3,4 };
std::vector<A> a(tmp, tmp+(sizeof(tmp)/sizeof(A)) );

I disagree.

Even an experienced programmer like yourself gets it, well not technically wrong
but pretty imperfect. :-(

If done, it would better look like this:

int const tmp[] = { 1,2,3,4 };
std::vector<A> a(tmp, tmp+(sizeof(tmp)/sizeof(*tmp)) );

You've been secretly reading c.l.c, haven't you Alf??

Sorry, I don't get it.

If you mean c.l.c++ (not the C language group), well I'm an old-timer here. You
should recognize me. I'm one of your humble clc++m moderators, so you can safely
assume that I know a bit of C++ and can cough up original arguments. :)


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top