473,395 Members | 1,652 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,395 software developers and data experts.

Array data members initialization

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[3];
public:
Example(const double & val0, const double & val1, const double &
val2); // yes, with doubles
...
};

and I want to write for the constructor something like

Example::Example(const double & val0, const double & val1, const double
& val2)
: array[0](val0), array[1](val1), array[2](val2)
{}

but obviously it does not work for me. I dont know what is the correct
syntax for the initialization, and if the member double array[3] MUST
be initialized with an array and no member by member as I did. Please
help me. Thank you. I am sorry for my English. Thank you again.

Sep 11 '06 #1
8 7312

iluvatar wrote:
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[3];
public:
Example(const double & val0, const double & val1, const double &
val2); // yes, with doubles
...
};

and I want to write for the constructor something like

Example::Example(const double & val0, const double & val1, const double
& val2)
: array[0](val0), array[1](val1), array[2](val2)
{}

but obviously it does not work for me. I dont know what is the correct
syntax for the initialization, and if the member double array[3] MUST
be initialized with an array and no member by member as I did. Please
help me. Thank you. I am sorry for my English. Thank you again.
Pass a reference to an array and use a reference to an array as a
member of you class.

class Example{
private:
const double (&array1) [3];
public:
Example( const double (&arr)[3]) : array1(arr)
{
}
};

int main(){
const double arrd[3] = { 3.0, 2.0, 1.0};
Example e(arrd);
return 0;
}

Sep 11 '06 #2

"iluvatar" <wo******@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hi all.

How can I initialize an array data member in the "faster" way?
Faster in what sense?
For example, suppose I have a class like

class Example{
private:
double array[3];
public:
Example(const double & val0, const double & val1, const double &
val2); // yes, with doubles
...
};

and I want to write for the constructor something like

Example::Example(const double & val0, const double & val1, const double
& val2)
: array[0](val0), array[1](val1), array[2](val2)
{}

but obviously it does not work for me. I dont know what is the correct
syntax for the initialization, and if the member double array[3] MUST
be initialized with an array and no member by member as I did. Please
help me. Thank you. I am sorry for my English. Thank you again.
You could use assignments in the constructor body:

Example::Example( const double & val0,
const double & val1, const double & val2)
{
array[0] = val0;
array[1] = val1;
array[2] = val2;
}

-Howard

Sep 11 '06 #3
iluvatar posted:
Example::Example(const double & val0, const double & val1, const double
& val2)
: array[0](val0), array[1](val1), array[2](val2)
{}

Take those parameters by value, not by reference -- a "double" doesn't
consume enough memory to warrant passing around its address rather than its
actual value. (Then again, your compiler might just compile as if you had
passed by value...)

You have stumbled across a defect in C++. When writing the C++ Standard,
the committee members focused all of their attention on adding new features
to the language, and neglected to refine the more basic features. We
_should_ be able to do something akin to the following:

class MyClass {
private:

double const arr[3];

public:

MyClass(double const a,double const b,double const c)
: arr( {a,b,c} )
{
/* Function Body */
}
};

, but alas we can't. The Standards Committee are apathetic when it comes to
remedying fundamental defects of this nature, so the defect may never be
resolved.

A possible solution might be to use compound literals (which are a feature
of C99):

: arr( (double[3]){a,b,c} )

Strictly speaking, they're not supported by C++, but most C++ compilers
support features of C99.

--

Frederick Gotham
Sep 11 '06 #4
Thank you Frederick. I use the gnu g++ compiler and the arr(
(double[3]){a,b,c} ) does not work. But the most important fact now is
your point of the lack for options such arr( {a,b,c} ) in the actual
standard. I will try to change my code to three data members replacing
the array. Bye.

Frederick Gotham wrote:
iluvatar posted:
Example::Example(const double & val0, const double & val1, const double
& val2)
: array[0](val0), array[1](val1), array[2](val2)
{}


Take those parameters by value, not by reference -- a "double" doesn't
consume enough memory to warrant passing around its address rather than its
actual value. (Then again, your compiler might just compile as if you had
passed by value...)

You have stumbled across a defect in C++. When writing the C++ Standard,
the committee members focused all of their attention on adding new features
to the language, and neglected to refine the more basic features. We
_should_ be able to do something akin to the following:

class MyClass {
private:

double const arr[3];

public:

MyClass(double const a,double const b,double const c)
: arr( {a,b,c} )
{
/* Function Body */
}
};

, but alas we can't. The Standards Committee are apathetic when it comes to
remedying fundamental defects of this nature, so the defect may never be
resolved.

A possible solution might be to use compound literals (which are a feature
of C99):

: arr( (double[3]){a,b,c} )

Strictly speaking, they're not supported by C++, but most C++ compilers
support features of C99.

--

Frederick Gotham
Sep 12 '06 #5
iluvatar posted:
Thank you Frederick. I use the gnu g++ compiler and the arr(
(double[3]){a,b,c} ) does not work.

It works for _me_ on g++. Maybe you need to upgrade your compiler?

But the most important fact now is your point of the lack for options
such arr( {a,b,c} ) in the actual standard. I will try to change my code
to three data members replacing the array. Bye.

If the array is non-const, you can simply put the code in the constructor
body:

MyClass::MyClass(double const a,double const b,double const c)
{
double *p = arr;

*p++ = a;
*p++ = b;
*p = c;
}

--

Frederick Gotham
Sep 12 '06 #6
iluvatar wrote:
Thank you Frederick.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian
Sep 12 '06 #7
iluvatar wrote:
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[3];
public:
Example(const double & val0, const double & val1, const double &
val2); // yes, with doubles
...
};

and I want to write for the constructor something like

Example::Example(const double & val0, const double & val1, const double
& val2)
: array[0](val0), array[1](val1), array[2](val2)
{}

but obviously it does not work for me. I dont know what is the correct
syntax for the initialization, and if the member double array[3] MUST
be initialized with an array and no member by member as I did.
Use a vector instead of an array (cf.
http://www.parashift.com/c++-faq-lit....html#faq-34.1) and an
initializer helper class:

#include <vector>
using namespace std;

template<typename T>
class Initializer
{
vector<Tv_;
public:
Initializer& Add( const T& t ) { v_.push_back(t); return *this; }
operator vector<T>() const { return v_; }
};

class Example
{
const vector<doublev_;
public:
Example( double d0, double d1, double d2 )
: v_( Initializer<double>()
.Add(d0)
.Add(d1)
.Add(d2) )
{}
// ...
};

Cheers! --M

Sep 12 '06 #8
Ohh, sorry. I was making a mistake. The (double[3]){a,b,c} ) also works
for me. But is more slow than the initial option. Bye

Frederick Gotham wrote:
iluvatar posted:
Thank you Frederick. I use the gnu g++ compiler and the arr(
(double[3]){a,b,c} ) does not work.


It works for _me_ on g++. Maybe you need to upgrade your compiler?

But the most important fact now is your point of the lack for options
such arr( {a,b,c} ) in the actual standard. I will try to change my code
to three data members replacing the array. Bye.


If the array is non-const, you can simply put the code in the constructor
body:

MyClass::MyClass(double const a,double const b,double const c)
{
double *p = arr;

*p++ = a;
*p++ = b;
*p = c;
}

--

Frederick Gotham
Sep 13 '06 #9

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

Similar topics

9
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the...
2
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
4
by: Stephen Mayes | last post by:
I have initialized an array like this. const char matrix = { {0, 1, 2, 3}, {0, 1, 2}, {0, 1} }; gcc, (with no options set,) errors unless I specify
3
by: Eric Laberge | last post by:
Aloha! I've been reading the standard (May '05 draft, actually) and stumbled across this: 6.7.1 Initialization §20 "If the aggregate or union contains elements or members that are aggregates...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
5
by: subramanian100in | last post by:
Consider the following classes without ctors. class Sample { private: double d_val; double* d_ptr; }; class Test
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.