C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

initializing a const array in a class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
J. Campbell
Guest





PostPosted: Wed Jan 28, 2004 3:29 pm    Post subject: initializing a const array in a class Reply with 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
Back to top
Gianni Mariani
Guest





PostPosted: Wed Jan 28, 2004 3:57 pm    Post subject: Re: initializing a const array in a class Reply with quote



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





PostPosted: Wed Jan 28, 2004 7:34 pm    Post subject: Re: initializing a const array in a class Reply with quote




"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





PostPosted: Wed Jan 28, 2004 10:08 pm    Post subject: Re: initializing a const array in a class Reply with quote

[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





PostPosted: Thu Jan 29, 2004 3:45 am    Post subject: Re: initializing a const array in a class Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.