How to initialized a member array?

P

Peng Yu

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;
}
 
E

EventHelix.com

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

Linlin Yan

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
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top