PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Initializing array of objects during declaration
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Initializing array of objects during declaration

Réponse
 
LinkBack Outils de la discussion
Vieux 30/06/2008, 18h03   #1
Peskov Dmitry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 ?
  Réponse avec citation
Vieux 30/06/2008, 23h48   #2
Ivan Novick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initializing array of objects during declaration

On Jun 30, 10:03am, Peskov Dmitry <vamsi.kom...@gmail.com> wrote:
> 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
  Réponse avec citation
Vieux 01/07/2008, 16h05   #3
Peskov Dmitry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initializing array of objects during declaration

On Jul 1, 3:48am, Ivan Novick <i...@novickmail.com> wrote:
> On Jun 30, 10:03am, Peskov Dmitry <vamsi.kom...@gmail.com> wrote:
>
>
>
> > 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.


  Réponse avec citation
Vieux 02/07/2008, 06h00   #4
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initializing array of objects during declaration

On Jul 1, 11:05 am, Peskov Dmitry <vamsi.kom...@gmail.com> wrote:
> On Jul 1, 3:48 am, Ivan Novick <i...@novickmail.com> wrote:
>
>
>
> > On Jun 30, 10:03 am, Peskov Dmitry <vamsi.kom...@gmail.com> wrote:

>
> > > 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.
  Réponse avec citation
Vieux 02/07/2008, 06h41   #5
tony_in_da_uk@yahoo.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initializing array of objects during declaration

On Jul 2, 12:05 am, Peskov Dmitry <vamsi.kom...@gmail.com> wrote:
> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15354 seconds with 13 queries