473,385 Members | 1,730 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,385 software developers and data experts.

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

Similar topics

7
by: news.hku.hk | last post by:
suppose i have a class abc, and i have an important value stored as int integer = 888; when i want to declare an array of objects: abc obj; // error said non-constant....expect constant...
7
by: Chandrashekar Tippur | last post by:
All, I need to initialize an array of structures but I don't know how may elements are there. I tried to malloc the array but I am not sure how to initialize them. Snippet: struct myarray{...
2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
3
by: farseer | last post by:
If i have an array of a certain type, is there away of initializing with without knowing it's type? for example (forgive me if some of this is syntactically incorrect) if i have a procedure...
31
by: arun | last post by:
suppose i have a pointer to an array of integers.can i initialize each member of the array using pointers?plz explain
2
by: mahesh_cbrl | last post by:
Hi, I am unable to intialize an array of structures and I keep getting an error. Here is how the data structure looks like. typedef struct { struct { int part_no; char item_name;
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.