Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #4664
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Ian Collins <ian-news@hotmail.com> |
| Newsgroups | comp.lang.c++ |
| Subject | Re: array of objects of parametrized constructor |
| Date | Wed, 04 May 2011 21:24:37 +1200 |
| Lines | 52 |
| Message-ID | <92ckamFj04U1@mid.individual.net> (permalink) |
| References | <f675e5f2-a0d6-471a-9d61-07fe605c7396@glegroupsg2000goo.googlegroups.com> <ippke2$6a7$1@dont-email.me> <92bdajFj03U16@mid.individual.net> <ipr4j8$r2v$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net scOq4zy5dTAYkpp71X3nqQPkf2rK7nf4RkEDpz/S8BMUTqrwi9 |
| Cancel-Lock | sha1:TRXnbymVaYIR6J0erWABtGYEFMs= |
| User-Agent | Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 |
| In-Reply-To | <ipr4j8$r2v$1@dont-email.me> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.c++:4664 |
Show key headers only | View raw
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
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web