Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #4633 > unrolled thread

array of objects of parametrized constructor

Started byasit <lipun4u@gmail.com>
First post2011-05-03 11:32 -0700
Last post2011-05-03 23:00 +0100
Articles 9 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  array of objects of parametrized constructor asit <lipun4u@gmail.com> - 2011-05-03 11:32 -0700
    Re: array of objects of parametrized constructor "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-03 21:29 +0200
      Re: array of objects of parametrized constructor Ian Collins <ian-news@hotmail.com> - 2011-05-04 10:18 +1200
        Re: array of objects of parametrized constructor "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-04 11:11 +0200
          Re: array of objects of parametrized constructor Ian Collins <ian-news@hotmail.com> - 2011-05-04 21:24 +1200
            Re: array of objects of parametrized constructor "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-04 11:42 +0200
              Re: array of objects of parametrized constructor Ian Collins <ian-news@hotmail.com> - 2011-05-04 21:34 +1200
    Re: array of objects of parametrized constructor Noah Roberts <dont@email.me> - 2011-05-03 14:27 -0700
      Re: array of objects of parametrized constructor Leigh Johnston <leigh@i42.co.uk> - 2011-05-03 23:00 +0100

#4633 — array of objects of parametrized constructor

Fromasit <lipun4u@gmail.com>
Date2011-05-03 11:32 -0700
Subjectarray of objects of parametrized constructor
Message-ID<f675e5f2-a0d6-471a-9d61-07fe605c7396@glegroupsg2000goo.googlegroups.com>
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.

[toc] | [next] | [standalone]


#4634

From"Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Date2011-05-03 21:29 +0200
Message-ID<ippke2$6a7$1@dont-email.me>
In reply to#4633
* 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

-- 
blog at <url: http://alfps.wordpress.com>

[toc] | [prev] | [next] | [standalone]


#4652

FromIan Collins <ian-news@hotmail.com>
Date2011-05-04 10:18 +1200
Message-ID<92bdajFj03U16@mid.individual.net>
In reply to#4634
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)) );

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#4662

From"Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Date2011-05-04 11:11 +0200
Message-ID<ipr4j8$r2v$1@dont-email.me>
In reply to#4652
* 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)) );

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

-- 
blog at <url: http://alfps.wordpress.com>

[toc] | [prev] | [next] | [standalone]


#4664

FromIan Collins <ian-news@hotmail.com>
Date2011-05-04 21:24 +1200
Message-ID<92ckamFj04U1@mid.individual.net>
In reply to#4662
On 05/ 4/11 09:11 PM, Alf P. Steinbach /Usenet wrote:
> * 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??

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#4666

From"Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Date2011-05-04 11:42 +0200
Message-ID<ipr6d0$ihi$1@dont-email.me>
In reply to#4664
* Ian Collins, on 04.05.2011 11:24:
> On 05/ 4/11 09:11 PM, Alf P. Steinbach /Usenet wrote:
>> * 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

-- 
blog at <url: http://alfps.wordpress.com>

[toc] | [prev] | [next] | [standalone]


#4667

FromIan Collins <ian-news@hotmail.com>
Date2011-05-04 21:34 +1200
Message-ID<92cku1Fj03U18@mid.individual.net>
In reply to#4666
On 05/ 4/11 09:42 PM, Alf P. Steinbach /Usenet wrote:
> * Ian Collins, on 04.05.2011 11:24:
>> On 05/ 4/11 09:11 PM, Alf P. Steinbach /Usenet wrote:
>>>
>>> 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. :-)

No, the C group.  The regs there love correcting sizeof(A) for 
sizeof(*tmp), its one of the group favourites!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#4640

FromNoah Roberts <dont@email.me>
Date2011-05-03 14:27 -0700
Message-ID<4dc0733e$0$2416$cc2e38e6@news.uslec.net>
In reply to#4633
On 5/3/2011 11:32 AM, asit wrote:
> 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));

-- 
http://crazycpp.wordpress.com

[toc] | [prev] | [next] | [standalone]


#4649

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-05-03 23:00 +0100
Message-ID<wJWdnew3g6F-513QnZ2dnUVZ8t6dnZ2d@giganews.com>
In reply to#4640
On 03/05/2011 22:27, Noah Roberts wrote:
> On 5/3/2011 11:32 AM, asit wrote:
>> 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));
>

You cannot see the wood for the trees (less so than Alf though):

A as[] = { 0, 1 };

:)

/Leigh

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web