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 

Question on const int as demension of an array

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





PostPosted: Mon Feb 27, 2006 2:06 am    Post subject: Question on const int as demension of an array Reply with quote



Shouldn't I be able to do this?

In file1.cpp:

extern const int aConst = 8;

= = = = = = = =

In file2.cpp

extern const int aConst;

class aClass
{
public:
char* foo[ aConst ];
};

= = = = = = = =

The compiler barfs on char* foo[ aConst } telling me that aConst isn't
a constant.

I know the above doesn't work in C but I'm pretty sure it's supposed
to work in C++.
Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Feb 27, 2006 2:06 am    Post subject: Re: Question on const int as demension of an array Reply with quote



* Eric:
Quote:

Shouldn't I be able to do this?

In file1.cpp:

extern const int aConst = 8;

= = = = = = = =

In file2.cpp

extern const int aConst;

class aClass
{
public:
char* foo[ aConst ];
};

= = = = = = = =

The compiler barfs on char* foo[ aConst } telling me that aConst isn't
a constant.

I know the above doesn't work in C but I'm pretty sure it's supposed
to work in C++.

The short of it is that you're using a link-time constant, wheras what's
required is a compile-time constant.

Of course the standard does not even acknowledge the existence of
compilers and linkers, except to the degree linking is suggested by
words such as "linkage".

But I think it's easier explain from the point of view of reality... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
benben
Guest





PostPosted: Mon Feb 27, 2006 3:06 am    Post subject: Re: Question on const int as demension of an array Reply with quote



Eric wrote:
Quote:

Shouldn't I be able to do this?

In file1.cpp:

extern const int aConst = 8;

= = = = = = = =

In file2.cpp

extern const int aConst;

class aClass
{
public:
char* foo[ aConst ];
};

= = = = = = = =

The compiler barfs on char* foo[ aConst } telling me that aConst isn't
a constant.

I know the above doesn't work in C but I'm pretty sure it's supposed
to work in C++.

The problem is the compiler can't know the value of aConst but needs to
decide sizeof(aClass).

So why not:

// In constdef.hpp

const int aConst = 8;



// In file2.cpp

#include "constdef.hpp"

clss aClass{
public: char* foo[aConst];
};


regards
ben
Back to top
Default User
Guest





PostPosted: Mon Feb 27, 2006 7:06 am    Post subject: Re: Question on const int as demension of an array Reply with quote

benben wrote:

Quote:
Eric wrote:

Shouldn't I be able to do this?

In file1.cpp:

extern const int aConst = 8;

= = = = = = = =

In file2.cpp

extern const int aConst;

class aClass
{
public:
char* foo[ aConst ];
};

= = = = = = = =

The compiler barfs on char* foo[ aConst } telling me that aConst
isn't a constant.

I know the above doesn't work in C but I'm pretty sure it's supposed
to work in C++.

The problem is the compiler can't know the value of aConst but needs
to decide sizeof(aClass).

So why not:

// In constdef.hpp

const int aConst = 8;



// In file2.cpp

#include "constdef.hpp"

clss aClass{
public: char* foo[aConst];
};


If constdef.hpp is included in more than one translation unit there
will be multiply defined aConst symbols. If it's only included in one,
you might as well declare a static in the file2.cpp.




Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Back to top
Markus Moll
Guest





PostPosted: Mon Feb 27, 2006 7:06 am    Post subject: Re: Question on const int as demension of an array Reply with quote

Hi

Default User wrote:

Quote:
If constdef.hpp is included in more than one translation unit there
will be multiply defined aConst symbols. If it's only included in one,
you might as well declare a static in the file2.cpp.

Constants have internal linkage (unless declared extern).

Markus
Back to top
Eric
Guest





PostPosted: Tue Feb 28, 2006 2:06 pm    Post subject: Re: Question on const int as demension of an array Reply with quote

Thanks to everyone who helped in this question.

I moved "const int aConst = 0" do a .h file to be #include'd where
needed and now all is fine.

I have this basic prejudice against putting things that allocate
memory into .h files, goes back to my C days, but as Markus and others
pointed out, "const int x" has internal linkage unless declared
"extern" so I guess in this case it doesn't matter.
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.