468,747 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,747 developers. It's quick & easy.

Initializing array of objects during declaration

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 ?
Jun 30 '08 #1
4 6994
On Jun 30, 10:03*am, Peskov Dmitry <vamsi.kom...@gmail.comwrote:
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
Jun 30 '08 #2
On Jul 1, 3:48*am, Ivan Novick <i...@novickmail.comwrote:
On Jun 30, 10:03*am, Peskov Dmitry <vamsi.kom...@gmail.comwrote:
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.
Jul 1 '08 #3
On Jul 1, 11:05 am, Peskov Dmitry <vamsi.kom...@gmail.comwrote:
On Jul 1, 3:48 am, Ivan Novick <i...@novickmail.comwrote:
On Jun 30, 10:03 am, Peskov Dmitry <vamsi.kom...@gmail.comwrote:
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.
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.
Jul 2 '08 #4
On Jul 2, 12:05 am, Peskov Dmitry <vamsi.kom...@gmail.comwrote:
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....signated-Inits

Cheers,

Tony
Jul 2 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by news.hku.hk | last post: by
7 posts views Thread by Chandrashekar Tippur | last post: by
2 posts views Thread by Raphael Iloh | last post: by
10 posts views Thread by Steve | last post: by
31 posts views Thread by arun | last post: by
2 posts views Thread by mahesh_cbrl | last post: by
6 posts views Thread by alacrite | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.