Array initializer in C++

W

Wu

Hi there,

I was wondering whether there is a way to use array initializer syntax to
initialize a non-static array member in a class in C++. In particular, if
I have a class Foo:

class Foo {
double arr[2];
}

How can I use C-like array intializer syntax:

double arr[2] = {1,2}

for the class?

Any help is appreciated.

--Peter
 
M

mlimber

Wu said:
Hi there,

I was wondering whether there is a way to use array initializer syntax to
initialize a non-static array member in a class in C++. In particular, if
I have a class Foo:

class Foo {
double arr[2];
}

How can I use C-like array intializer syntax:

double arr[2] = {1,2}

for the class?

Any help is appreciated.

--Peter

You cannot use that syntax with non-static members unless the data is
public and there are no constructors. Of course, constructors are to be
preferred (cf. http://www.parashift.com/c++-faq-lite/ctors.html). Also,
arrays are evil
(http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp.lang.c++/msg/e2fe5982913d4414

Cheers! --M
 
W

Wu

Then basically I cannot have a non-static const array member in a class in C++? For example,

class Foo {
const double arr[2];
}

A 'const' array has to be intialized using initializer and a 'const'
non-static member requires a class to have a constructor. And then with
the information you gave, you really cannot have a non-static const
array member in a class in C++.

Is this right? Thanks!

Wu said:
Hi there,

I was wondering whether there is a way to use array initializer syntax to
initialize a non-static array member in a class in C++. In particular, if
I have a class Foo:

class Foo {
double arr[2];
}

How can I use C-like array intializer syntax:

double arr[2] = {1,2}

for the class?

Any help is appreciated.

--Peter

You cannot use that syntax with non-static members unless the data is
public and there are no constructors. Of course, constructors are to be
preferred (cf. http://www.parashift.com/c++-faq-lite/ctors.html). Also,
arrays are evil
(http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp.lang.c++/msg/e2fe5982913d4414

Cheers! --M
 
M

mlimber

Wu said:
Wu said:
Hi there,

I was wondering whether there is a way to use array initializer syntax to
initialize a non-static array member in a class in C++. In particular, if
I have a class Foo:

class Foo {
double arr[2];
}

How can I use C-like array intializer syntax:

double arr[2] = {1,2}

for the class?

Any help is appreciated.

--Peter

You cannot use that syntax with non-static members unless the data is
public and there are no constructors. Of course, constructors are to be
preferred (cf. http://www.parashift.com/c++-faq-lite/ctors.html). Also,
arrays are evil
(http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp.lang.c++/msg/e2fe5982913d4414

Cheers! --M
Then basically I cannot have a non-static const array member in a class in C++? For example,

class Foo {
const double arr[2];
}

A 'const' array has to be intialized using initializer and a 'const'
non-static member requires a class to have a constructor. And then with
the information you gave, you really cannot have a non-static const
array member in a class in C++.

Is this right? Thanks!

Please put your response below the text you are responding to.
Top-posting is considered impolite. (I fixed it here.)

You are correct that you cannot do that innately. You could, however,
get around the limitation by using a const std::vector as in my
previously cited post or by using a boost::scoped_array and the
appropriate initializer like that given in the previously cited post.

Cheers! --M
 
V

Victor Bazarov

Wu said:
Then basically I cannot have a non-static const array member in a class in C++? For example,

class Foo {
const double arr[2];
}

A 'const' array has to be intialized using initializer and a 'const'
non-static member requires a class to have a constructor. And then with
the information you gave, you really cannot have a non-static const
array member in a class in C++.

Is this right?

(a) Don't top-post.

(b) Yes, this is right.

V
 
W

Wu

mlimber said:
Wu said:
Wu wrote:
Hi there,

I was wondering whether there is a way to use array initializer syntax to
initialize a non-static array member in a class in C++. In particular, if
I have a class Foo:

class Foo {
double arr[2];
}

How can I use C-like array intializer syntax:

double arr[2] = {1,2}

for the class?

Any help is appreciated.

--Peter

You cannot use that syntax with non-static members unless the data is
public and there are no constructors. Of course, constructors are to be
preferred (cf. http://www.parashift.com/c++-faq-lite/ctors.html). Also,
arrays are evil
(http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp.lang.c++/msg/e2fe5982913d4414

Cheers! --M
Then basically I cannot have a non-static const array member in a class in C++? For example,

class Foo {
const double arr[2];
}

A 'const' array has to be intialized using initializer and a 'const'
non-static member requires a class to have a constructor. And then with
the information you gave, you really cannot have a non-static const
array member in a class in C++.

Is this right? Thanks!

Please put your response below the text you are responding to.
Top-posting is considered impolite. (I fixed it here.)

You are correct that you cannot do that innately. You could, however,
get around the limitation by using a const std::vector as in my
previously cited post or by using a boost::scoped_array and the
appropriate initializer like that given in the previously cited post.

Cheers! --M

Hi mlimber, Victor and all,

Sorry for the top-posting. I didn't mean it (I am new to newsgroup, so I
didn't know this convention).

Thanks a lot for your quick responses!

--Peter
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top