473,569 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2286
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_a rray 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_a rray 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
6491
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); // A 5x5x5 3-dimensional matrix with each element initialized to 1 Array< Array <Array<int> > > Matrix3D (5,1);
1
413
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 = { "w", "x", "y", "z", 0 }; const char* const* test = { t1, t2, t3, 0 }; I was wondering whether the is a more elegant way of writing such an
1
7356
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 = {{L"onLoad"},{L"onQueryTerminate"},{L"onTerminate"},{L""}}; i am getting the following errors:
16
41752
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, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the...
20
7065
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 string literal.
15
4855
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 the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with...
18
2471
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
7321
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, const double & val1, const double &
14
2601
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 var arr2 = new Array(); I've been a-googling, but the best reason for this that I can find is
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.