Initializing array of objects during declaration

P

Peskov Dmitry

class simple_class
{
int data;
public:
simple_class() {data=10;};
simple_class(int val) : data(val){}
};

int main()
{
simple_class obj1(10); // Initializing a single object through
single parameter constructor
return 0;
}

How to initialize the values of array of simple_class objects during
declaration through the single paramter constructor?

1 method i know is

simple_class obj_array[]={5,6,10};

However, I would like to know if there is any alternative way so that
i can initialize things while declaring an array of objects ?
 
I

Ivan Novick

class simple_class
{
   int data;
   public:
   simple_class() {data=10;};
   simple_class(int val) : data(val){}

};

int main()
{
   simple_class obj1(10);  // Initializing a single object through
single parameter constructor
   return 0;

}

How to initialize the values of array of simple_class objects during
declaration through the single paramter constructor?

1 method i know is

simple_class obj_array[]={5,6,10};

However, I would like to know if there is any alternative way so that
i can initialize things while declaring an array of objects ?

What is wrong with the code you showed? That is perfectly valid way
to initialize objects in an arary:

simple_class obj_array[3] = {5,6,10};

Ivan Novick
http://www.mycppquiz.com
 
P

Peskov Dmitry

class simple_class
{
   int data;
   public:
   simple_class() {data=10;};
   simple_class(int val) : data(val){}

int main()
{
   simple_class obj1(10);  // Initializing a single object through
single parameter constructor
   return 0;

How to initialize the values of array of simple_class objects during
declaration through the single paramter constructor?
1 method i know is
simple_class obj_array[]={5,6,10};
However, I would like to know if there is any alternative way so that
i can initialize things while declaring an array of objects ?

What is wrong with the code you showed?  That is perfectly valid way
to initialize objects in an arary:

simple_class obj_array[3] = {5,6,10};

Ivan Novickhttp://www.mycppquiz.com


I am looking for any alternative way of declaring for arrays in
general something like...

simple_class obj_array[10](100); // I know this syntax is wrong.
will intialize all the 10 objects to 100 using a single parameter . I
need a similar syntax.
 
S

Salt_Peter

class simple_class
{
int data;
public:
simple_class() {data=10;};
simple_class(int val) : data(val){}
};
int main()
{
simple_class obj1(10); // Initializing a single object through
single parameter constructor
return 0;
}
How to initialize the values of array of simple_class objects during
declaration through the single paramter constructor?
1 method i know is
simple_class obj_array[]={5,6,10};
However, I would like to know if there is any alternative way so that
i can initialize things while declaring an array of objects ?
What is wrong with the code you showed? That is perfectly valid way
to initialize objects in an arary:
simple_class obj_array[3] = {5,6,10};

I am looking for any alternative way of declaring for arrays in
general something like...

simple_class obj_array[10](100); // I know this syntax is wrong.
will intialize all the 10 objects to 100 using a single parameter . I
need a similar syntax.

Its time you start digging into STL containers, both sequenced and
associative containers.
Look at std::vector in this case.
Beware if you do - you'll rarely ever use an array again.

#include <iostream>
#include <vector>

class simple
{
int data;
public:
simple() : data(0) { }
simple(int val) : data(val) { }
};

int main()
{
std::vector< simple > container(1000, 10);
std::cout << container.size() << std::endl;
}

/*
1000
*/

And your container now has 1000 elements all with the precious member
initialized to 10
That vector is dynamic (it can grow / resize).
If i was to step through all the reasons why a vector is a much better
choice than an array, this post would be humongous. Array means more
code, more work. The Vector is a workhorse, simple, and quite
versatile.
 
T

tony_in_da_uk

I am looking for any alternative way of declaring for arrays in
general something like...

simple_class obj_array[10](100); // I know this syntax is wrong.
will intialize all the 10 objects to 100 using a single parameter . I
need a similar syntax.

While Peter's already given sound advice, if you happen to be using
GNU g++ or a compiler with compatible extensions and don't care about
portability, you may find this "neat"...

http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Designated-Inits.html#Designated-Inits

Cheers,

Tony
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top