473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array initializer in C++

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


Dec 14 '05 #1
5 2275
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-lit...html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp....fe5982913d4414

Cheers! --M

Dec 14 '05 #2
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!

On Wed, 14 Dec 2005, mlimber wrote:
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-lit...html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp....fe5982913d4414

Cheers! --M

Dec 14 '05 #3
Wu wrote:
On Wed, 14 Dec 2005, mlimber wrote:
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-lit...html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp....fe5982913d4414

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

Dec 14 '05 #4
Wu wrote:
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
Dec 14 '05 #5
Wu

mlimber wrote:
Wu wrote:
On Wed, 14 Dec 2005, mlimber wrote:
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-lit...html#faq-34.1). See
this post for two similar methods for initializing std::vectors
instead:

http://groups.google.com/group/comp....fe5982913d4414

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


Dec 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hrmadhu | last post by:
Hi, I am trying to write a template for an array which I can use to create multi-dimensional matrices as // A 10x10 matrix with each element initialized to 0 Array< Array<int> > Matrix (10,0);...
1
by: Steve | last post by:
I want an initializer for an array of pointers to arrays of strings. So I can do something like this: const char* t1 = { "a", "b", "c", 0 }; const char* t2 = { "p", "q", 0 }; const char* t3 =...
1
by: Saurabh Aggrawal | last post by:
Hi, On line nos. 24, 25, 26 24: wstring cfMethods = {{L"setLabel"},{L""}}; 25: wstring cfProperties= {{L"isVisible"},{L""}}; 26: wstring cfEvents =...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
18
by: Michael Press | last post by:
Hello. I am puzzled. A line of the form char array = { a}; or char array = { a, b, c}; is an array initializer.
8
by: iluvatar | last post by:
Hi all. How can I initialize an array data member in the "faster" way? For example, suppose I have a class like class Example{ private: double array; public: Example(const double & val0,...
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.