how to create an array of objects initializing each object?

V

vaysagekv

hi,
how to create an array of objects initializing each object?
thanks in advance
 
V

Victor Bazarov

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
 
P

Paul N

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.
 
V

Victor Bazarov

See FAQ 10.5.


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
 
V

Virchanza

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;
 
Y

Yakov Gerlovin

*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;
}
 
V

Victor Bazarov

*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
 
Y

Yakov Gerlovin

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)
 
Y

Yakov Gerlovin

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)
 
V

Victor Bazarov

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
 
Y

Yakov Gerlovin

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)?
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top