473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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++.
Feb 27 '06 #1
5 1415
* 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... ;-)

--
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?
Feb 27 '06 #2
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];
};
regards
ben
Feb 27 '06 #3
benben wrote:
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)
Feb 27 '06 #4
Hi

Default User wrote:
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

Feb 27 '06 #5

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.
Feb 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
14
by: subramanian100in | last post by:
What is meant by stable qsort ?
6
by: Ronald Raygun | last post by:
I want to be able to randomly select the following from an array: 1). An image 2). A piece of text (name of tge image) 3). A piece of text (description of the image) I want to be able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.