Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7831 > unrolled thread
| Started by | vaysagekv <kvvaysage@gmail.com> |
|---|---|
| First post | 2011-07-15 17:42 +0530 |
| Last post | 2011-07-19 11:08 -0400 |
| Articles | 13 — 6 participants |
Back to article view | Back to comp.lang.c++
how to create an array of objects initializing each object? vaysagekv <kvvaysage@gmail.com> - 2011-07-15 17:42 +0530
Re: how to create an array of objects initializing each object? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-15 08:21 -0400
Re: how to create an array of objects initializing each object? Paul N <gw7rib@aol.com> - 2011-07-16 10:58 -0700
Re: how to create an array of objects initializing each object? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-16 16:13 -0400
Re: how to create an array of objects initializing each object? Virchanza <virtual@lavabit.com> - 2011-07-17 14:50 -0700
Re: how to create an array of objects initializing each object? Yakov Gerlovin <yakov.gerlovin@gmail.com> - 2011-07-18 04:24 -0700
Re: how to create an array of objects initializing each object? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-18 08:59 -0400
Re: how to create an array of objects initializing each object? Yakov Gerlovin <yakov.gerlovin@gmail.com> - 2011-07-18 08:17 -0700
Re: how to create an array of objects initializing each object? Yakov Gerlovin <yakov.gerlovin@gmail.com> - 2011-07-18 08:51 -0700
Re: how to create an array of objects initializing each object? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-18 13:34 -0400
Re: how to create an array of objects initializing each object? Leigh Johnston <leigh@i42.co.uk> - 2011-07-18 19:00 +0100
Re: how to create an array of objects initializing each object? Yakov Gerlovin <yakov.gerlovin@gmail.com> - 2011-07-19 06:56 -0700
Re: how to create an array of objects initializing each object? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-19 11:08 -0400
| From | vaysagekv <kvvaysage@gmail.com> |
|---|---|
| Date | 2011-07-15 17:42 +0530 |
| Subject | how to create an array of objects initializing each object? |
| Message-ID | <ivparg$e19$2@dont-email.me> |
hi,
how to create an array of objects initializing each object?
thanks in advance
[toc] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-07-15 08:21 -0400 |
| Message-ID | <ivpbc5$grt$3@dont-email.me> |
| In reply to | #7831 |
On 7/15/2011 8:12 AM, vaysagekv wrote: > how to create an array of objects initializing each object? You can initialize them if you provide a brace-enclosed initialiser list for the array. You can't do it with a dynamic array, however. V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Paul N <gw7rib@aol.com> |
|---|---|
| Date | 2011-07-16 10:58 -0700 |
| Message-ID | <d27b659c-e81f-47ef-a212-b0a22bb3adee@z14g2000yqh.googlegroups.com> |
| In reply to | #7834 |
On Jul 15, 1:21 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote: > On 7/15/2011 8:12 AM, vaysagekv wrote: > > > how to create an array of objects initializing each object? See FAQ 10.5. > You can initialize them if you provide a brace-enclosed initialiser list > for the array. You can't do it with a dynamic array, however. Well, if you use a dynamic array everything does get initialised with the default constructor. Which may be what the OP wanted. If you want the elements of the array to be different, you can do this by mesing about with a static member or a global.
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-07-16 16:13 -0400 |
| Message-ID | <ivsrcn$e3c$1@dont-email.me> |
| In reply to | #7883 |
On 7/16/2011 1:58 PM, Paul N wrote: > On Jul 15, 1:21 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote: >> On 7/15/2011 8:12 AM, vaysagekv wrote: >> >>> how to create an array of objects initializing each object? > > See FAQ 10.5. > >> You can initialize them if you provide a brace-enclosed initialiser list >> for the array. You can't do it with a dynamic array, however. > > Well, if you use a dynamic array everything does get initialised with > the default constructor. Actually, that's not entirely accurate. If there is the initializer in the form "()", it's default-initialized, if there isn't, an array of built-in types will go *uninitialized*. > Which may be what the OP wanted. If you want > the elements of the array to be different, you can do this by mesing > about with a static member or a global. V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Virchanza <virtual@lavabit.com> |
|---|---|
| Date | 2011-07-17 14:50 -0700 |
| Message-ID | <eb1ed31e-004a-4902-b4c5-795323247778@dp9g2000vbb.googlegroups.com> |
| In reply to | #7834 |
On Jul 15, 1:21 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> You can initialize them if you provide a brace-enclosed initialiser list
> for the array. You can't do it with a dynamic array, however.
If you want to go to extremes:
char unsigned *const cu_pointer = new char
unsigned[ sizeof(MyClass) * N ];
MyClass *const p = reinterpret_cast<MyClass*>(cu_pointer);
::new( p ) MyClass(1,2,3);
::new( p+1 ) MyClass(4,5,6);
::new( p+2 ) MyClass(7,8,9);
// and then at the end:
p[2].~MyClass();
p[1].~MyClass();
p->~MyClass();
delete [] cu_pointer;
[toc] | [prev] | [next] | [standalone]
| From | Yakov Gerlovin <yakov.gerlovin@gmail.com> |
|---|---|
| Date | 2011-07-18 04:24 -0700 |
| Message-ID | <00e7d0de-bb61-473c-a6ed-ce0e63532014@a1g2000yqp.googlegroups.com> |
| In reply to | #7831 |
*If you have tr1*
#include <array>
#include <iostream>
using namespace std;
struct MyClass
{
MyClass(int i){
cout << "MyClass(int " << i << ")" << endl;
}
MyClass(const char* s) {
cout << "MyClass(const char* " << s << ")" << endl;
}
MyClass(int i,int j, int k) {
cout << "MyClass(this=" << this << " i=" << i << " j=" << j << "
k="<< k << ")" << endl;
}
};
void main()
{
array<MyClass,4> arr = { 1, 2, "str", MyClass(1,2,3) };
cout << "Address of elem at pos 3 is " << &(arr[3]) << endl;
}
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-07-18 08:59 -0400 |
| Message-ID | <j01ao8$vqq$1@dont-email.me> |
| In reply to | #7931 |
On 7/18/2011 7:24 AM, Yakov Gerlovin wrote:
> *If you have tr1*
>
> #include<array>
> #include<iostream>
>
> using namespace std;
>
> struct MyClass
> {
> MyClass(int i){
> cout<< "MyClass(int "<< i<< ")"<< endl;
> }
> MyClass(const char* s) {
> cout<< "MyClass(const char* "<< s<< ")"<< endl;
> }
> MyClass(int i,int j, int k) {
> cout<< "MyClass(this="<< this<< " i="<< i<< " j="<< j<< "
> k="<< k<< ")"<< endl;
> }
> };
>
> void main()
int main()
> {
> array<MyClass,4> arr = { 1, 2, "str", MyClass(1,2,3) };
> cout<< "Address of elem at pos 3 is "<< &(arr[3])<< endl;
> }
And... isn't this still copy-initialization (and not direct
initialization) of the array elements?
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Yakov Gerlovin <yakov.gerlovin@gmail.com> |
|---|---|
| Date | 2011-07-18 08:17 -0700 |
| Message-ID | <5813b2fe-ff87-4c6f-8da8-184313a6aea5@c29g2000yqd.googlegroups.com> |
| In reply to | #7934 |
> And... isn't this still copy-initialization (and not direct > initialization) of the array elements? I'm not sure what exactly do you mean by "direct initialization". If it is compile time initialization, then naturally it is not direct initialization. If it means, that the object are created in-place, without creation of temp objects and then copy construction, then it is a direct initialization. At least with g++ 4.3.2. ( I'm not sure whether standard requires a temp objects in this case or it allows in-place object creation)
[toc] | [prev] | [next] | [standalone]
| From | Yakov Gerlovin <yakov.gerlovin@gmail.com> |
|---|---|
| Date | 2011-07-18 08:51 -0700 |
| Message-ID | <4eb31bd0-fe9c-45c5-9c7f-f266384834c3@s2g2000vbw.googlegroups.com> |
| In reply to | #7935 |
For PODs it is possible to initialize at compile time:
struct POD
{
int i;
const char* s;
};
array<POD,2> arr2 = { 1, "str1", 2,"str2" };
(like Victor said in his first post)
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-07-18 13:34 -0400 |
| Message-ID | <j01qrv$j72$1@dont-email.me> |
| In reply to | #7935 |
On 7/18/2011 11:17 AM, Yakov Gerlovin wrote: >> And... isn't this still copy-initialization (and not direct >> initialization) of the array elements? > > I'm not sure what exactly do you mean by "direct initialization". > If it is compile time initialization, then naturally it is not direct > initialization. > > If it means, that the object are created in-place, without creation of > temp objects and then copy construction, then it is a direct > initialization. At least with g++ 4.3.2. ( I'm not sure whether > standard requires a temp objects in this case or it allows in-place > object creation) I mean, if you define a copy-ctor in your class (instead of letting the compiler define one), you might see whether it's called at all during initialization of the array. V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-07-18 19:00 +0100 |
| Message-ID | <PJWdnSgtgYRS6bnTnZ2dnUVZ8mednZ2d@giganews.com> |
| In reply to | #7940 |
On 18/07/2011 18:34, Victor Bazarov wrote: > On 7/18/2011 11:17 AM, Yakov Gerlovin wrote: >>> And... isn't this still copy-initialization (and not direct >>> initialization) of the array elements? >> >> I'm not sure what exactly do you mean by "direct initialization". >> If it is compile time initialization, then naturally it is not direct >> initialization. >> >> If it means, that the object are created in-place, without creation of >> temp objects and then copy construction, then it is a direct >> initialization. At least with g++ 4.3.2. ( I'm not sure whether >> standard requires a temp objects in this case or it allows in-place >> object creation) > > I mean, if you define a copy-ctor in your class (instead of letting the > compiler define one), you might see whether it's called at all during > initialization of the array. Well you can do that if you make the copy ctor private and see if you get a compilation error message (you should as it is copy-initialization). Just adding a non-private copy ctor does not prove anything though as an implementation is free to elide the copy as an optimization. /Leigh
[toc] | [prev] | [next] | [standalone]
| From | Yakov Gerlovin <yakov.gerlovin@gmail.com> |
|---|---|
| Date | 2011-07-19 06:56 -0700 |
| Message-ID | <63ae01e3-dc81-46d9-a52a-16e4819b3a1f@m18g2000vbl.googlegroups.com> |
| In reply to | #7942 |
Leigh is right, g++ requires a public copy constructor, but does not actually call it I made a copy constructor with some prints and it had not been called. Making it private gives a compiler error. The question is how can optimizer elide function with side effects (prints)?
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-07-19 11:08 -0400 |
| Message-ID | <j046le$qel$1@dont-email.me> |
| In reply to | #7970 |
On 7/19/2011 9:56 AM, Yakov Gerlovin wrote: > Leigh is right, g++ requires a public copy constructor, but does not > actually call it > > I made a copy constructor with some prints and it had not been called. > Making it private gives a compiler error. > > The question is how can optimizer elide function with side effects > (prints)? Elision of calling the copy constructor is explicitly allowed by the Standard regardless of its side effects. V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web