Question on const int as demension of an array

E

Eric

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++.
 
A

Alf P. Steinbach

* Eric:
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... ;-)
 
B

benben

Eric said:
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
 
D

Default User

benben said:
Eric said:
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
 
M

Markus Moll

Hi

Default said:
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
 
E

Eric

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top