Connecting Tech Pros Worldwide Forums | Help | Site Map

How to initialized a member array?

Peng Yu
Guest
 
Posts: n/a
#1: Sep 4 '08
Hi,

I'm wondering if there is a way to initialized a member array just as
the initialization of a member variable? Or I have to initialized the
array inside the function body of the constructor?

Thanks,
Peng

#include <iostream>

struct A {
A() : array({0, 1, 2, 3}), variable(100) { }
int array[4];
int variable;
};


int main() {
A a;
std::cout << a.array[0] << std::endl;
std::cout << a.variable << std::endl;
}

EventHelix.com
Guest
 
Posts: n/a
#2: Sep 4 '08

re: How to initialized a member array?


On Sep 3, 9:25*pm, Peng Yu <PengYu...@gmail.comwrote:
Quote:
Hi,
>
I'm wondering if there is a way to initialized a member array just as
the initialization of a member variable? Or I have to initialized the
array inside the function body of the constructor?
>
Thanks,
Peng
>
#include <iostream>
>
struct A {
* A() : array({0, 1, 2, 3}), variable(100) { }
* int array[4];
* int variable;
>
};
>
int main() {
* A a;
* std::cout << a.array[0] << std::endl;
* std::cout << a.variable << std::endl;
>
}
C++ does not support member array initialization via the initializer
list.

--
http://www.EventHelix.com/EventStudio
Sequence diagram based systems engineering tool
Linlin Yan
Guest
 
Posts: n/a
#3: Sep 4 '08

re: How to initialized a member array?


On Sep 4, 9:25*am, Peng Yu <PengYu...@gmail.comwrote:
Quote:
Hi,
>
I'm wondering if there is a way to initialized a member array just as
the initialization of a member variable? Or I have to initialized the
array inside the function body of the constructor?
>
Thanks,
Peng
>
#include <iostream>
>
struct A {
* A() : array({0, 1, 2, 3}), variable(100) { }
* int array[4];
* int variable;
>
};
>
int main() {
* A a;
* std::cout << a.array[0] << std::endl;
* std::cout << a.variable << std::endl;
>
}
>
>
Waiting for c++0x
Closed Thread