| View previous topic :: View next topic |
| Author |
Message |
Eric Guest
|
Posted: Mon Feb 27, 2006 2:06 am Post subject: Question on const int as demension of an array |
|
|
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
|
Posted: Mon Feb 27, 2006 2:06 am Post subject: Re: Question on const int as demension of an array |
|
|
* 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
|
Posted: Mon Feb 27, 2006 3:06 am Post subject: Re: Question on const int as demension of an array |
|
|
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
|
Posted: Mon Feb 27, 2006 7:06 am Post subject: Re: Question on const int as demension of an array |
|
|
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
|
Posted: Mon Feb 27, 2006 7:06 am Post subject: Re: Question on const int as demension of an array |
|
|
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
|
Posted: Tue Feb 28, 2006 2:06 pm Post subject: Re: Question on const int as demension of an array |
|
|
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 |
|
 |
|