 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
J. Campbell Guest
|
Posted: Wed Jan 28, 2004 3:29 pm Post subject: initializing a const array in a class |
|
|
I have a class that I want to contain a const data-member equivalent to eg:
int array[8] = {7,11,13,17,19,23,29,31};
What's the simplest way to create such a table inside a class?
Thanks,
Joe
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Wed Jan 28, 2004 3:57 pm Post subject: Re: initializing a const array in a class |
|
|
J. Campbell wrote:
| Quote: | I have a class that I want to contain a const data-member equivalent to eg:
int array[8] = {7,11,13,17,19,23,29,31};
What's the simplest way to create such a table inside a class?
Thanks,
Joe
|
How about this ?
const int array[8] = {7,11,13,17,19,23,29,31};
struct foo
{
const int (&m_array)[8];
foo()
: m_array( array )
{
}
};
|
|
| Back to top |
|
 |
Joe C Guest
|
Posted: Wed Jan 28, 2004 7:34 pm Post subject: Re: initializing a const array in a class |
|
|
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote
| Quote: | J. Campbell wrote:
I have a class that I want to contain a const data-member equivalent to
eg:
int array[8] = {7,11,13,17,19,23,29,31};
What's the simplest way to create such a table inside a class?
Thanks,
Joe
How about this ?
const int array[8] = {7,11,13,17,19,23,29,31};
struct foo
{
const int (&m_array)[8];
foo()
: m_array( array )
{
}
};
|
Gianni, Thanks. This works;-) I'm a little confused by the syntax. What
exactly does:
const int (&m_array)[8]
mean? and how does it differ from a:
const int* to the head of an array?
Thanks for your help/solution.
Joe
|
|
| Back to top |
|
 |
S. Richards Guest
|
Posted: Wed Jan 28, 2004 10:08 pm Post subject: Re: initializing a const array in a class |
|
|
[email]mango_maniac (AT) yahoo (DOT) com[/email] (J. Campbell) wrote in message news:<b97c86e1.0401280729.6bb82949 (AT) posting (DOT) google.com>...
| Quote: | I have a class that I want to contain a const data-member equivalent to eg:
int array[8] = {7,11,13,17,19,23,29,31};
What's the simplest way to create such a table inside a class?
Thanks,
Joe
|
You can make it static and initialize it as follows:
class A
{
public:
static const int iarr[2];
};
const int A::iarr[2] = { 0, 1 };
Sheldon.
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Thu Jan 29, 2004 3:45 am Post subject: Re: initializing a const array in a class |
|
|
Joe C wrote:
| Quote: | "Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote in message
news:bv8m63$476 (AT) dispatch (DOT) concentric.net...
J. Campbell wrote:
I have a class that I want to contain a const data-member equivalent to
eg:
int array[8] = {7,11,13,17,19,23,29,31};
What's the simplest way to create such a table inside a class?
Thanks,
Joe
How about this ?
const int array[8] = {7,11,13,17,19,23,29,31};
struct foo
{
const int (&m_array)[8];
foo()
: m_array( array )
{
}
};
Gianni, Thanks. This works;-) I'm a little confused by the syntax. What
exactly does:
const int (&m_array)[8]
mean?
|
it says: m_array is a reference to an 8 element array of const int.
and how does it differ from a:
| Quote: | const int* to the head of an array?
|
Little except that in the case of the array reference, the compiler
"knows" the size of the array - if there was code that depended on the
"array" instead of "pointer" behaviour, you preserved it's meaning using
the reference. You can also use this information to eliminate some
overrun errors.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|